问题
I've integrated the zxing library into my app. I have a runtime error when i try to scan a QR code. It's caused by a NPE when trying to inflate a ViewFinderView object from XML. In my project all my app's code resides in a package called com.carefreegroup and all the zxing code resides in com.google.zxing.client.android. I've done some logging to test whether the ViewFinderView is null and it does in fact come back as null.
Can anyone tell me why this object is not getting inflated, could it be a package problem?
01-02 15:09:53.887: E/AndroidRuntime(14732): FATAL EXCEPTION: main
01-02 15:09:53.887: E/AndroidRuntime(14732): java.lang.RuntimeException: Unable to resume activity {com.carefreegroup/com.google.zxing.client.android.CaptureActivity}: java.lang.NullPointerException
01-02 15:09:53.887: E/AndroidRuntime(14732): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2850)
01-02 15:09:53.887: E/AndroidRuntime(14732): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2889)
01-02 15:09:53.887: E/AndroidRuntime(14732): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
01-02 15:09:53.887: E/AndroidRuntime(14732): at android.app.ActivityThread.access$600(ActivityThread.java:139)
01-02 15:09:53.887: E/AndroidRuntime(14732): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
01-02 15:09:53.887: E/AndroidRuntime(14732): at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 15:09:53.887: E/AndroidRuntime(14732): at android.os.Looper.loop(Looper.java:156)
01-02 15:09:53.887: E/AndroidRuntime(14732): at android.app.ActivityThread.main(ActivityThread.java:5045)
01-02 15:09:53.887: E/AndroidRuntime(14732): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 15:09:53.887: E/AndroidRuntime(14732): at java.lang.reflect.Method.invoke(Method.java:511)
01-02 15:09:53.887: E/AndroidRuntime(14732): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-02 15:09:53.887: E/AndroidRuntime(14732): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-02 15:09:53.887: E/AndroidRuntime(14732): at dalvik.system.NativeStart.main(Native Method)
01-02 15:09:53.887: E/AndroidRuntime(14732): Caused by: java.lang.NullPointerException
01-02 15:09:53.887: E/AndroidRuntime(14732): at com.google.zxing.client.android.CaptureActivity.onResume(CaptureActivity.java:192)
01-02 15:09:53.887: E/AndroidRuntime(14732): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1236)
01-02 15:09:53.887: E/AndroidRuntime(14732): at android.app.Activity.performResume(Activity.java:4625)
01-02 15:09:53.887: E/AndroidRuntime(14732): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2834)
01-02 15:09:53.887: E/AndroidRuntime(14732): ... 12 more
I can post my manifest file if needed to check packages.
Thanks in advance
回答1:
You've copied the project's UI including CaptureActivity
and ViewfinderView
. Don't do that -- it's not OK and not wholly permitted. You need to write your own app and reuse parts that you understand, into an app that you understand. Here indeed you pasted a bunch of code incompletely, and it doesn't work when you do that.
Integrate by Intent, or write your own app. In any event please do not use com.google.zxing.client.android
like this.
来源:https://stackoverflow.com/questions/14124329/android-zxing-captureactivity-npe-on-viewfinderview