1

I was reluctant to post this question after banging my head against my desk and carving though related post on stackoverflow, but just can't get the problem resolved. So here we are. I am trying integrate ZXing into my Android app as many developers have attempted but have consistently gotten the same error: Unable to start activity ComponentInfo. I have core.jar added and have added the activity and camera permissions to the manifest. I know there is something very simple I am missing to do here. I get the following error when trying to scan using intent. I have already tried going through the ZXing library and changing all switch statements to if/else. I have also tried adding my package name in the ZXing layouts like suggested here: Android ZXing implementation

Java:

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainMenu extends Activity {

Button btn_scan

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);

    btn_scan = (Button) findViewById(R.id.btn_scanbarcode);
    btn_scan.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(
                    "com.google.zxing.client.android.SCAN");
            intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
            startActivityForResult(intent, 0);

        }
    });

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            // Handle successful scan
        } else if (resultCode == RESULT_CANCELED) {
            // Handle cancel
        }
    }
}

}

Manifest

<activity
        android:name="com.google.zxing.client.android.CaptureActivity"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="landscape"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:windowSoftInputMode="stateAlwaysHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.google.zxing.client.android.SCAN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

LogCat:

11-30 10:54:04.713: E/AndroidRuntime(15452): FATAL EXCEPTION: main
11-30 10:54:04.713: E/AndroidRuntime(15452): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.testapp/com.google.zxing.client.android.CaptureActivity}: android.view.InflateException: Binary XML file line #27: Error inflating class com.android.testapp
11-30 10:54:04.713: E/AndroidRuntime(15452):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2240)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2275)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at android.os.Looper.loop(Looper.java:156)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at android.app.ActivityThread.main(ActivityThread.java:5060)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at java.lang.reflect.Method.invokeNative(Native Method)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at java.lang.reflect.Method.invoke(Method.java:511)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at dalvik.system.NativeStart.main(Native Method)
11-30 10:54:04.713: E/AndroidRuntime(15452): Caused by: android.view.InflateException: Binary XML file line #27: Error inflating class com.android.testapp
11-30 10:54:04.713: E/AndroidRuntime(15452):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:278)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at android.app.Activity.setContentView(Activity.java:1897)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at com.google.zxing.client.android.CaptureActivity.onCreate(CaptureActivity.java:143)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at android.app.Activity.performCreate(Activity.java:4543)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2196)
11-30 10:54:04.713: E/AndroidRuntime(15452):    ... 11 more
11-30 10:54:04.713: E/AndroidRuntime(15452): Caused by: java.lang.ClassNotFoundException: com.android.testapp
11-30 10:54:04.713: E/AndroidRuntime(15452):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at android.view.LayoutInflater.createView(LayoutInflater.java:552)
11-30 10:54:04.713: E/AndroidRuntime(15452):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
11-30 10:54:04.713: E/AndroidRuntime(15452):    ... 21 more
Community
  • 1
  • 1
AndroidNewb
  • 441
  • 3
  • 6
  • 14
  • Somewhere in one of your layouts, there is something called `testapp`. Please post the corresponding XML. It's likely part of the problem. – Codo Nov 30 '12 at 17:46
  • Just trying to clarify. You want to see the ZXing layouts or the layout where i am calling the ZXing intent? – AndroidNewb Nov 30 '12 at 18:20

2 Answers2

1

There are several problems here, among them that you are trying to declare our activity and namespace in your app. Please do not do this. It doesn't work for you and causes problems for our users.

You seem to want to do this: https://github.com/zxing/zxing/wiki/Scanning-Via-Intent

If so it's way easier than you are making it here.

Rune Vikestad
  • 4,642
  • 1
  • 25
  • 36
Sean Owen
  • 66,182
  • 23
  • 141
  • 173
  • Sean I have read your comments throughout stackoverflow discouraging the use of Zxing in this way. I really appreciate your work and plan on giving your team full credit for this functionality in the about section of the app. The truth is some users do not have the Scanner app installed to their devices. Does this ScanningViaIntent support devices that do no already have the scanner app installed and do not wish to download it? – AndroidNewb Nov 30 '12 at 19:14
  • 1
    I'm not discouraging this approach per se. I'm discouraging 1) near complete copy and paste, 2) any practice that interferes with our users due to bad manifest entries, which come up usually because of #1, and 3) wasting your time building your own not-copied UI if you don't need it. It's not just a question of getting credit or not; you are already required by the Apache License to do that if reusing the code anyway. Yes, the Intent mechanism will help users install the app if it's not present already. – Sean Owen Nov 30 '12 at 19:20
  • 1
    Thanks for the response Sean. It does not answer my original question. I do not intend to just do a copy and paste of your code. My only aim was to get it working before i begin making changes to the actual code you provided. I don't mind building my own UI either. Many developers have already done this and i was hoping they could lead me in the right direction. Please don't penalize us, many users will already have the Barcode scanner app installed and some will not. I just want to cover those who do not and aren't open to downloading an extra app. I understand if you do not want to help. – AndroidNewb Nov 30 '12 at 19:37
  • I am pretty lost then. You say you want to build your own app, but, your code above is plainly trying to use Intents. I'm directing how to make that work. You need to clarify your question if this is not what you're asking. – Sean Owen Nov 30 '12 at 21:04
  • My app will have a button clicked where it will launch barcode scanner via intent. If barcode scanner is not on the device instead of asking user to download it, it should use zxing code embedded in app via the com.google.zxing.client.android library. – AndroidNewb Nov 30 '12 at 21:32
1

You have import zxing project as a library and strip out classes you don't need. This process was painful but i got it working.

AndroidNewb
  • 441
  • 3
  • 6
  • 14