getIntent 返回 null - Android

用户6150316

我有一个包含三个活动(MainActivity、PlayerActivity 和 ListActivity)的项目。我想从发送意图MainActivityPlayerActivity选择一个名称PlayerActivity,并再次把它传递给MainActivity.

我写了另一个意图将一些数据发送到ListActivityfrom Mainactivity

MainActivityListActivity作品的完美,也MainActivityPlayerActivity.但当PlayerActivity发送意向MainActivity,我得到一个空的意图。

这是我的代码:

从 ListActivity 发送:

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = getIntent();
            Bundle bundle = intent.getExtras();

            int buttonId = bundle.getInt("buttonID");
            String name = parent.getItemAtPosition(position).toString();

            Intent sendIntentToMainActivity = new Intent(PlayersActivity.this, MainActivity.class);
            Bundle bundle1 = new Bundle();

            bundle1.putString("name", name);
            bundle1.putInt("buttonId", buttonId);

            startActivity(sendIntentToMainActivity);

        }
    });

进入主活动:

@Override
protected void onNewIntent(Intent intent) {
    if (intent != null) {
        setIntent(intent);
        Log.d("xxx", "Intent is null");
    }
}

@Override
protected void onResume() {
    super.onResume();

    Intent intent = getIntent();
    Bundle bundle = new Bundle();

    int id = bundle.getInt("buttonID");
    String name = bundle.getString("name");

    if (id == mButton_first_group_frist_name.getId()) {
        mButton_first_group_frist_name.setText(name);
    } else if (id == mButton_first_group_second_name.getId()) {
        mButton_first_group_second_name.setText(name);
    } else if (id == mButton_second_group_frist_name.getId()) {
        mButton_second_group_frist_name.setText(name);
    } else if (id == mButton_second_group_second_name.getId()) {
        mButton_second_group_second_name.setText(name);
    }
}

请帮助并提供建议

格拉蒂安·阿辛巴韦

您必须将包添加到创建的意图中,并在返回 onResume 时从意图中获取包。你可以这样做:

Intent sendIntentToMainActivity = new Intent(PlayersActivity.this, MainActivity.class);
            Bundle bundle1 = new Bundle();

            bundle1.putString("name", name);
            bundle1.putInt("buttonId", buttonId);


            sendIntentToMainActivity.putExtra("bundle",bundle1);
            startActivity(sendIntentToMainActivity);

然后在里面onResume

Intent intent = getIntent();
    Bundle bundle = intent.getBundleExtra("bundle");

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android Studio getIntent()。getStringExtra()返回null

来自分类Dev

getIntent返回null

来自分类Dev

getIntent()始终返回null

来自分类Dev

getIntent返回null

来自分类Dev

getIntent()。getStringExtra()最终返回null

来自分类Dev

getIntent()。getStringExtra()最终返回null

来自分类Dev

如果NAME不等于“分数”,则getIntent()。getStringArrayListExtra(NAME)返回null

来自分类Dev

调用活动被销毁时,新活动上的Android getIntent为NULL

来自分类Dev

调用活动被销毁时,新活动上的Android getIntent为NULL

来自分类Dev

android:LruCache返回null

来自分类Dev

Android:findViewById返回null

来自分类Dev

Android ProgressDialog返回null

来自分类Dev

Android UsbAccessory []返回null

来自分类Dev

android:LruCache返回null

来自分类Dev

Android getAllCellInfo()返回null

来自分类Dev

Android-片段中的getIntent()

来自分类Dev

在onActivityResult中,getIntent()。getSerializableExtra为null

来自分类Dev

Bundle = getIntent().getExtras() 始终为 null

来自分类Dev

Android相机照片返回null

来自分类Dev

Android的KeyStore getKey返回null

来自分类Dev

Android-GsonRequest返回null

来自分类Dev

findFragmentByTag在android中返回null

来自分类Dev

Cordova Android Filetransfer返回null

来自分类Dev

Android Webview Cookie返回null

来自分类Dev

Android-findViewById返回null

来自分类Dev

getLastKnownLocation在Android中返回null

来自分类Dev

Android MapView getMap()返回null

来自分类Dev

Android:支持MapFragment返回null

来自分类Dev

Android片段-findViewById返回null