请帮忙!当我运行我的程序时,当我进入第二个活动时,一切正常,但是当我回到第一个活动时,它会出现错误。以下是完整的错误消息:
Process: com.example.android.lifeofcrime, PID: 2826
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=42, result=0, data=null} to activity {com.example.android.lifeofcrime/com.example.android.lifeofcrime.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4053)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
at com.example.android.lifeofcrime.MainActivity.onActivityResult(MainActivity.java:63)
at android.app.Activity.dispatchActivityResult(Activity.java:6917)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4049)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
以下是第一项活动的代码:
public class MainActivity extends AppCompatActivity {
int test = 5;
TextView money;
int testReturn2 = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
money = (TextView) findViewById (R.id.money);
setupWorkButton();
}
public void setupWorkButton() {
Button workButton = (Button) findViewById(R.id.workButton);
workButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, workScreen.class);
intent.putExtra("testing", test);
startActivityForResult(intent, 42);
}
});
}
// Gets called when the activity started finishes
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-Generate method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 42:
testReturn2 = data.getIntExtra("the answer", 0);
money.setText("" + testReturn2);
break;
}
}
}
以下是第二项活动的代码:
public class workScreen extends AppCompatActivity {
int test;
int testReturn = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_screen);
Intent intent = getIntent();
test = intent.getIntExtra("testing", 0);
TextView displayTest = (TextView) findViewById(R.id.intView);
displayTest.setText("" + test);
setupCharacterButton();
}
public void setupCharacterButton() {
Button characterButton = (Button) findViewById(R.id.characterButton);
characterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
testReturn = test * 2;
intent.putExtra("the answer", testReturn);
finish();
}
});
}
}
答案 0 :(得分:0)
你的意图== null。好像你忘了在第二个活动中设置活动结果:
setResult(Activity.RESULT_OK, intent);
在完成第二个活动之前输入此代码