今天突发奇想做了一个安卓界面的实验。实验的大体思想如下。
1在mainActivity的layout中添加一个button1
2在secondActivity的onCreate中添加一个代码。
Button button =(Button) findViewById(R.id.button1);
button.setText("dddddddddd");
然后在启动主程序
实验结果如下:
主程序启动没有任何异常。但是在按钮调用secondActiviey的时候出现程序崩溃。
得到的错误代码如下
07-14 12:19:30.507 6882-6907/com.example.bleuesprit.test W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xb431bc40, error=EGL_SUCCESS
07-14 12:19:54.503 6882-6882/com.example.bleuesprit.test D/AndroidRuntime﹕ Shutting down VM
07-14 12:19:54.504 6882-6882/com.example.bleuesprit.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.bleuesprit.test, PID: 6882
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bleuesprit.test/com.example.bleuesprit.test.secondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
at com.example.bleuesprit.test.secondActivity.onCreate(secondActivity.java:20)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
07-14 12:19:54.550 6882-6895/com.example.bleuesprit.test I/art﹕ WaitForGcToComplete blocked for 45.527ms for cause Background
07-14 12:19:54.807 6882-6890/com.example.bleuesprit.test W/art﹕ Suspending all threads took: 30.132ms
07-14 12:19:55.006 6882-6895/com.example.bleuesprit.test I/art﹕ Background partial concurrent mark sweep GC freed 881(96KB) AllocSpace objects, 0(0B) LOS objects, 50% free, 995KB/2019KB, paused 183.236ms total 450.860ms
07-14 12:19:55.046 6882-6895/com.example.bleuesprit.test W/art﹕ Suspending all threads took: 40.097ms
关键的信息就是一行
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bleuesprit.test/com.example.bleuesprit.test.secondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
空指针异常。原因是button是个空指针。所以setText的时候出现错误。
原因:
我估计在secondActivity的时候。之前的layout已经释放资源,或者secondActivity并不能看得到。这样button是一个空指针。
正确做法。
1mainActivity调用startActivityForResult函数来调取secondActivity
。
Intent intent =new Intent(MainActiveity.this,secondActivity.class);
startActivityForResult(intent, 1);
2secondActivity 调用setResult设置返回的intend,其中dddddd可以放在这个结果intend中。调用finish()结束
Intent intent = new Intent();
intent.putExtra("buttonText", "ddddddd");
setResult(RESULT_OK, intent);
finish();
3mainActivity重写onActivityResult来进行更改操作
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
Button button =(Button)findViewById(R.id.button1);
String text=getIntent().getStringExtra("buttonText");
button.setText(text);
}
break;
default:
}
}