在Active.onCreate()中,为什么 Intent.getExtras() 有时会返回 null?
2022-09-02 01:43:24
这可能是一个假警报,看看我自己的答案。原始问题如下:
活动具有一个按钮,可将用户带到另一个活动。为了启动新活动,我们用extras填充我们的Intent,然后onCreate(),新活动通过Intent.getExtras()从这些附加内容中读取。我们假设返回的捆绑包将是非空的,但是随着客户崩溃报告的发现,getExtras() 有时会返回 null。
正如这个答案所示,空保护附加功能是完全可以的,但是如果你填充了意图的附加内容,那么为什么它以后会返回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)