2014-09-01 131 views
0

我使用此代碼重新加載我以前的活動,當我點擊後退按鈕但我的應用程序崩潰時。它得到空指針異常。什麼可能是這裏的問題?如何從其他活動重新加載活動

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event){ 
    keyCode= KeyEvent.KEYCODE_BACK; 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     Intent i=getIntent(); 
     final Bundle b = i.getExtras(); 
     Intent in = new Intent(this,UserProductActivity.class); 
     in.putExtra("id", Integer.parseInt(b.get("id").toString())); 
     in.putExtra("itemname", b.get("itemname").toString()); 
     in.putExtra("amt", b.get("amt").toString()); 
     in.putExtra("clientCode", b.get("code").toString()); 
     in.putExtra("day", b.get("day").toString()); 
     setResult(0,in); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 

logcat的

09-01 10:46:32.656: E/AndroidRuntime(4356): FATAL EXCEPTION: main 
09-01 10:46:32.656: E/AndroidRuntime(4356): java.lang.NullPointerException 
09-01 10:46:32.656: E/AndroidRuntime(4356):  at com.example.senditem.UserItemDetailActivity.onKeyDown(UserItemDetailActivity.java:139) 
09-01 10:46:32.656: E/AndroidRuntime(4356):  at android.view.KeyEvent.dispatch(KeyEvent.java:2641) 
09-01 10:46:32.656: E/AndroidRuntime(4356):  at android.app.Activity.dispatchKeyEvent(Activity.java:2393) 
09-01 10:46:32.656: E/AndroidRuntime(4356):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2023) 
09-01 10:46:32.656: E/AndroidRuntime(4356):  at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:4118) 
09-01 10:46:32.656: E/AndroidRuntime(4356):  at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:4061) 
09-01 10:46:32.656: E/AndroidRuntime(4356):  at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3144) 
09-01 10:46:32.656: E/AndroidRuntime(4356):  at android.os.Handler.dispatchMessage(Handler.java:107) 
09-01 10:46:32.656: E/AndroidRuntime(4356):  at android.os.Looper.loop(Looper.java:194) 
09-01 10:46:32.656: E/AndroidRuntime(4356):  at android.app.ActivityThread.main(ActivityThread.java:5400) 
09-01 10:46:32.656: E/AndroidRuntime(4356):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-01 10:46:32.656: E/AndroidRuntime(4356):  at java.lang.reflect.Method.invoke(Method.java:525) 
09-01 10:46:32.656: E/AndroidRuntime(4356):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:837) 
09-01 10:46:32.656: E/AndroidRuntime(4356):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604) 
09-01 10:46:32.656: E/AndroidRuntime(4356):  at dalvik.system.NativeStart.main(Native Method) 
+0

在UserItemDetailActivity.java的第139行有什麼? – 2014-09-01 04:38:47

+0

是否使用'startActivityForResult'? – 2014-09-01 04:39:48

+0

是的我在之前的活動中使用startActivityForResult,因爲我傳遞的是我的意圖數據。我想要的是將我的數據從最後一個活動傳回到我以前的活動,這就是爲什麼我使用setResult – phoenix 2014-09-01 05:15:52

回答

0

我想像的關鍵之一在你與b.get()查詢包不存在

+0

yah.I其實懷疑它,我刪除了其他鍵,但它仍然得到空指針異常 – phoenix 2014-09-01 05:17:26

0
  private String id = ""; 
     private String itemname = ""; 
     private String amt = ""; 
     private String code = ""; 
     private String day = ""; 
    @Override 
public boolean onKeyDown(int keyCode, KeyEvent event){ 
keyCode= KeyEvent.KEYCODE_BACK; 
if (keyCode == KeyEvent.KEYCODE_BACK) { 

    Intent in = new Intent(getApplicationContext(),UserProductActivity.class); 
    //eg.if you id,itemname,amt,code,day from EditText 
     // edtid = (EditText) findViewById(R.id.edtId);etc.. 

     this.id = edtid.getText().toString(); 
     this.itemname = edtitemname.getText().toString(); 
     this.amt = edtamt.getText().toString(); 
     this.code = edtcode.getText().toString(); 
     this.day = edtday.getText().toString(); 
    in.putExtra("id", this.id); 
    in.putExtra("itemname", this.itemname); 
    in.putExtra("amt", this.amt); 
    in.putExtra("clientCode", this.code); 
    in.putExtra("day", this.day); 
    startActivity(in); 
    } 
return super.onKeyDown(keyCode, event); 
} 

//UserProductActiviy.class

Bundle extras = intent.getExtras(); 
    String id = extras.getString("id");ect...