这可能是一场虚惊,请参阅我自己的答案。下面的原始问题:
一个活动有一个将用户带到另一个活动的按钮。为了启动新的活动,我们用额外的内容填充我们的 Intent,然后 onCreate(),新的活动通过 Intent.getExtras() 从这些额外的内容中读取。我们假设返回的捆绑包是非空的,但随着客户崩溃报告的发现,getExtras() 有时会返回空。
正如这个答案所示,对附加内容进行空保护是非常好的,但是如果您填充了意图的附加内容,那么为什么它以后会返回 null 呢?有没有更好的地方(比如 onResume())来阅读附加内容?
编辑:这可能是因为我们没有遵循键所需的名称约定:
该名称必须包含包前缀,例如应用程序 com.android.contacts 将使用类似“com.android.contacts.ShowAll”的名称。
这是来自Intent.putExtras javadoc。如果您不遵循此名称约定会发生什么;行为是否已定义?
以下是相关代码:
class FromActivity extends Activity {
ImageButton button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.from_view);
button = (ImageButton)findViewById(R.id.button);
button.setVisibility(View.VISIBLE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(FromActivity.this, ToActivity.class);
i.putExtra(ToActivity.SERVER_PARAM, ...);
i.putExtra(ToActivity.UUID_PARAM, ...);
i.putExtra(ToActivity.TEMPLATE_PARAM, ...);
startActivityForResult(i, 0);
overrideTransition(R.anim.slide_left_in, R.anim.slide_left_out);
}
});
}
}
class ToActivity extends Activity {
public static final String SERVER_PARAM = "server";
public static final String UUID_PARAM = "uuid";
public static final String TEMPLATE_PARAM = "template";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras == null) {
finish();
return;
}
// do stuff with extras
}
}
这是此问题的示例堆栈跟踪:
java.lang.RuntimeException: Unable to start activity ComponentInfo{ToActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
at android.app.ActivityThread.access$600(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at ToActivity.onCreate(SourceFile:49)
at android.app.Activity.performCreate(Activity.java:5066)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
... 11 more
java.lang.NullPointerException
at ToActivity.onCreate(SourceFile:49)
at android.app.Activity.performCreate(Activity.java:5066)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
at android.app.ActivityThread.access$600(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
at dalvik.system.NativeStart.main(Native Method)