如何从另一个Activity重新加载Activity

时间:2014-09-01 02:55:30

标签: android-intent

当我点击后退按钮但我的应用程序崩溃时,我使用此代码重新加载我之前的活动。它会出现空指针异常。这可能是什么问题?

@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)

2 个答案:

答案 0 :(得分:0)

我想象一下你用b.get()

查询的包中没有其中一个键。

答案 1 :(得分: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...