Android ZXing Integration无法启动活动错误

时间:2012-11-30 17:23:01

标签: android zxing

我不愿意在把头撞到我的桌子上并在stackoverflow上雕刻相关帖子之后发布这个问题,但是无法解决问题。我们在这里。我正在尝试将 ZXing 集成到我的Android应用中,因为许多开发人员都尝试过,但却始终遇到同样的错误:Unable to start activity ComponentInfo。我添加了core.jar,并为清单添加了活动和摄像头权限。我知道这里有一些非常简单的事情。尝试使用intent进行扫描时出现以下错误。我已经尝试通过ZXing库并将所有switch语句更改为if / else。我还尝试在ZXing布局中添加我的包名称,如下所示:Android ZXing implementation

爪哇:

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
        }
    }
}

}

清单

<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

2 个答案:

答案 0 :(得分:1)

这里有几个问题,其中包括您试图在应用中声明我们的活动和命名空间。请不要这样做。它不适合您并给我们的用户带来问题。

您似乎想要这样做:https://github.com/zxing/zxing/wiki/Scanning-Via-Intent

如果是这样的话比你在这里做的容易。

答案 1 :(得分:1)

您将导入zxing项目作为库并删除不需要的类。这个过程很痛苦,但我确实有效。