Android 为什么活动getIntent().getExtras()有时会返回null?
我有一个活动,在罕见的情况下,它的Android 为什么活动getIntent().getExtras()有时会返回null?,android,Android,我有一个活动,在罕见的情况下,它的getIntent().getExtras()将返回null public class NewNoteChecklistLauncherFragmentActivity extends AppCompatActivity { private static final String NEW_NOTE_CHECKLIST_LAUNCHER_FRAGMENT = "NEW_NOTE_CHECKLIST_LAUNCHER_FRAGMENT"; priv
getIntent().getExtras()
将返回null
public class NewNoteChecklistLauncherFragmentActivity extends AppCompatActivity {
private static final String NEW_NOTE_CHECKLIST_LAUNCHER_FRAGMENT = "NEW_NOTE_CHECKLIST_LAUNCHER_FRAGMENT";
private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.mAppWidgetId = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
FragmentManager fm = getSupportFragmentManager();
NewNoteChecklistLauncherFragment newNoteChecklistLauncherFragment = (NewNoteChecklistLauncherFragment) fm.findFragmentByTag(NEW_NOTE_CHECKLIST_LAUNCHER_FRAGMENT);
if (newNoteChecklistLauncherFragment == null) {
Bundle bundle = this.getIntent().getExtras();
if (bundle == null) {
// WHY?
throw new java.lang.RuntimeException();
}
我不确定这是怎么发生的。每次,我都会启动
活动
,调用putExtra
Intent i = new Intent(context, NewNoteChecklistLauncherFragmentActivity.class);
Note note = new Note();
i.putExtra(NewNoteChecklistLauncherFragment.INTENT_EXTRA_NOTE, note);
同时,它还充当“共享”操作的意图过滤器
我不能制造这样的问题。但是,有一段时间,我可以看到这样的事情在生产中发生
任何想法,在什么情况下,
getIntent().getExtras()
将在上述情况下返回null
。这可能是一个错误的ACTION\u SEND
实现,无法附加任何额外内容。这可能是一些自动脚本-或脚本小子-手动调用您的活动,而不需要任何额外的东西
因为活动是导出的,所以我推荐一些“防御性编程”。不要假设存在额外的
捆绑包
。在这种情况下,请尽量“优雅地降级”。此活动是否已导出?一些修补匠是否可以自己启动活动,无论是从另一个应用程序还是adb
?哦,是的。我忘了提一下,它还充当
。这可能是一个原因吗?但是,当我从外部应用程序共享时,并没有崩溃。这很公平。如果包为空,我将调用this.finish()
。
<activity android:name="com.yocto.wenote.note.NewNoteChecklistLauncherFragmentActivity"
android:theme="@style/Theme.Transparent"
android:windowSoftInputMode="stateAlwaysHidden" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>