【问题标题】:Bundle is null after setting it in IntentBundle 在 Intent 中设置后为空
【发布时间】:2016-04-29 01:39:54
【问题描述】:

我知道有这样的问题: android-intent-bundle-always-nullintent-bundle-returns-null-every-time 但没有正确答案。

在我的Activity 1

public void goToMapView(Info info) {
    Intent intent = new Intent(getApplicationContext(), MapViewActivity.class);
    //intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    intent.putExtra("asdf", true);
    info.write(intent);
    startActivity(intent);
}

在信息中:

public void write(Intent intent) {
    Bundle b = new Bundle();
    b.putInt(AppConstants.ID_KEY, id);
    ... //many other attributes
    intent.putExtra(AppConstants.BUNDLE_NAME, b);
}
public static Info read(Bundle bundle) {
    Info info = new Info();
    info.setId(bundle.getInt(AppConstants.ID_KEY));
    ... //many other attributes
    return info;
}

在 MapViewActivity (Activity 2) 中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_view);

    Bundle extras = getIntent().getBundleExtra(AppConstants.BUNDLE_NAME);
    info = Info.read(extras);
    ...
}

问题是extras bundle 始终为空。我已经对其进行了调试,并且 Intent (intent = getIntent()) 的所有字段都设置为 null,除了一个指示这是什么类 (MapViewActivity) 的字段。

我也尝试通过intent.putExtras(b) 放置捆绑包,效果相同。 intent.putExtra("asdf", true) 仅用于调试原因 - 我也无法获取此数据(因为 getIntent() 几乎所有字段都设置为空)。

编辑

以下答案正确且有效。这是我的错。我没有正确地将我的捆绑包传递给新意图。

【问题讨论】:

    标签: android android-intent null bundle


    【解决方案1】:

    我不确定“信息”的用途,但我建议在涉及其他数据对象之前先将数据从一个活动传递到另一个活动。

    活动1

        Intent intent = new Intent(Activity1.this, Activity2.class);
        intent.putExtra("asdf", true);
        info.write(intent);
        startActivity(intent);
    

    活动2

        Bundle bundle = getIntent.getExtras();
        if (bundle!=null) {
            if(bundle.containsKey("asdf") {
                boolean asdf = bundle.getBooleanExtra("asdf");
                Log.i("Activity2 Log", "asdf:"+String.valueOf(asdf));
            }
        } else {
            Log.i("Activity2 Log", "asdf is null");
    
        }
    

    【讨论】:

    • Info 类用于汇总我需要的信息。它具有静态“读取”和实例“写入”方法,以提高可读性。最基本的数据传递是通过放置“asdf”来表示的,这不起作用。你的意思是getBoolean 而不是getBooleanExtra?没有这样的方法。我只是检查你的例子 - 我得到asdf is null。不过还是谢谢。
    【解决方案2】:

    活动一

    Intent intent = new Intent(getApplicationContext(), MapViewActivity.class);
    
            Bundle b = new Bundle();
             b.putBoolean("asdf", true);
             b.putInt(AppConstants.ID_KEY, id);
             intent.putExtras(b);
    
             startActivity(intent);
    

    活动 2

    Bundle extras = getIntent().getExtras();
    
     boolean bool = extras.getBoolean("asdf");
     int m_int = extras.getInt(AppConstants.ID_KEY,-1);
    

    【讨论】:

    • 我已经尝试过,因为它在底部有问题:“我也尝试过通过intent.putExtras(b) 放置捆绑包,效果相同”。 Info 类没有任何问题。
    • 以上工作正常。无论如何尝试将值直接放在意图中,例如 intent.putExtra("asdf",false);intent.putExtra(AppConstants.ID_KEY,id);
    猜你喜欢
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    相关资源
    最近更新 更多