Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么活动getIntent().getExtras()有时会返回null?_Android - Fatal编程技术网

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>