如果用户从我的应用程序(意图)启动ZBY条形码扫描器,而不是按下回退按钮,我该如何捕获?-它显示“对不起,我的应用程序被迫关闭”(或多或少,因为我的语言不是英语)。
现在我有:
if (scanResult != null) {
intent.putExtra("com.tajchert.kolejrogowska.SCAN.SCAN_MODE", "QR_CODE_MODE");
String contents = intent.getStringExtra("SCAN_RESULT");
whatToDo(contents);
}
else{
finish();
}
如果没有
else{}
块,它是一样的。编辑:日志:
11-25 00:16:33.738: E/AndroidRuntime(21969): FATAL EXCEPTION: main
11-25 00:16:33.738: E/AndroidRuntime(21969): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=49374, result=0, data=null} to activity {com.tajchert.kolejrogowska/com.tajchert.kolejrogowska.Kody}: java.lang.NullPointerException
11-25 00:16:33.738: E/AndroidRuntime(21969): at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
11-25 00:16:33.738: E/AndroidRuntime(21969): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
11-25 00:16:33.738: E/AndroidRuntime(21969): at android.app.ActivityThread.access$2800(ActivityThread.java:125)
11-25 00:16:33.738: E/AndroidRuntime(21969): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
11-25 00:16:33.738: E/AndroidRuntime(21969): at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 00:16:33.738: E/AndroidRuntime(21969): at android.os.Looper.loop(Looper.java:123)
11-25 00:16:33.738: E/AndroidRuntime(21969): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-25 00:16:33.738: E/AndroidRuntime(21969): at java.lang.reflect.Method.invokeNative(Native Method)
11-25 00:16:33.738: E/AndroidRuntime(21969): at java.lang.reflect.Method.invoke(Method.java:521)
11-25 00:16:33.738: E/AndroidRuntime(21969): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-25 00:16:33.738: E/AndroidRuntime(21969): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-25 00:16:33.738: E/AndroidRuntime(21969): at dalvik.system.NativeStart.main(Native Method)
11-25 00:16:33.738: E/AndroidRuntime(21969): Caused by: java.lang.NullPointerException
11-25 00:16:33.738: E/AndroidRuntime(21969): at com.tajchert.kolejrogowska.Kody.onActivityResult(Kody.java:77)
11-25 00:16:33.738: E/AndroidRuntime(21969): at android.app.Activity.dispatchActivityResult(Activity.java:3890)
11-25 00:16:33.738: E/AndroidRuntime(21969): at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
11-25 00:16:33.738: E/AndroidRuntime(21969): ... 11 more
最佳答案
键入以下内容:
if (scanResult != null) {
String contents = scanResult.getContents();
if(contents != null)
whatToDo(contents);
else
finish();
}