0
好吧,我試圖調用startActivityForResult方法,但由於某種原因,NullPointerException不斷彈出。我得到了堆棧回溯,但無法弄清楚問題出在哪裏。當調用activity.startActivityForResult(Java)時出現NullPointerException錯誤(Java)(Android)
W/System.err(6973): java.lang.NullPointerException
W/System.err(6973): at android.app.Activity.startActivityForResult(Activity.java:3505)
W/System.err(6973): at android.app.Activity.startActivityForResult(Activity.java:3466)
W/System.err(6973): at plugin.ZXing.integrator.IntentIntegrator.initiateScan(IntentIntegrator.java:88)
W/System.err(6973): at plugin.ZXing.integrator.IntentIntegrator.initiateScan(IntentIntegrator.java:31)
W/System.err(6973): at plugin.ZXing.ZXingJarActivity.show(ZXingJarActivity.java:24)
W/System.err(6973): at plugin.ZXing.LuaLoader$1.run(LuaLoader.java:238)
W/System.err(6973): at android.os.Handler.handleCallback(Handler.java:733)
W/System.err(6973): at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err(6973): at android.os.Looper.loop(Looper.java:157)
W/System.err(6973): at android.app.ActivityThread.main(ActivityThread.java:5872)
W/System.err(6973): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(6973): at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err(6973): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
W/System.err(6973): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
W/System.err(6973): at dalvik.system.NativeStart.main(Native Method)
另外這裏是我的代碼:(initiateScan(活性,NULL,NULL,NULL);是線31和activity.startActivityForResult(意圖,REQUEST_CODE);被線88)
public static void initiateScan(Activity activity) {
if (activity == null){
Log.d("ZXING PLUGIN", "ACTIVITY IS NULL (1)");
}
initiateScan(activity, null, null, null);
}
public static void initiateScan(Activity activity, String scanFormatsString,
String characterSet, ZXingLibConfig config) {
if (activity == null){
Log.d("ZXING PLUGIN", "ACTIVITY IS NULL (2)");
}
Intent intent = new Intent(activity, CaptureActivity.class);
if (intent == null){
Log.d("ZXING PLUGIN", "INTENT IS NULL (1)");
}
intent.putExtra(Intents.Scan.FORMATS, scanFormatsString);
intent.putExtra(Intents.Scan.CHARACTER_SET, characterSet);
intent.putExtra(ZXingLibConfig.INTENT_KEY, config);
try{
activity.startActivityForResult(intent, REQUEST_CODE);
} catch (NullPointerException e){
e.printStackTrace();
}
}
另外這裏就是我所說的功能
public class ZXingJarActivity extends Activity {
private Handler handler = new Handler();
public int show() {
IntentIntegrator.initiateScan(ZXingJarActivity.this);
return 0;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE:
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode,
resultCode, data);
if (scanResult == null) {
return;
}
final String result = scanResult.getContents();
if (result != null) {
handler.post(new Runnable() {
@Override
public void run() {
Log.d("ZXING PLUGIN", result);
}
});
}
break;
default:
}
}
}
這是我的初始化庫:
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
ZXingJarActivity ActivityClass = new ZXingJarActivity();
ActivityClass.show();
}
});
這兩個變量都顯示爲空,所以我很困惑它爲什麼不工作。如果有人可以幫我找出錯誤我會很感激:)
表示88行@IntentIntegrator' –
你怎麼稱呼意圖? – Skynet
你是如何實例化你作爲參數傳遞的'Activity'的? – laalto