捆绑在意图设置后空 [英] Bundle is null after setting it in Intent

查看:155
本文介绍了捆绑在意图设置后空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有问题,如:
<一href=\"http://stackoverflow.com/questions/8097267/android-intent-bundle-always-null\">android-intent-bundle-always-null和<一个href=\"http://stackoverflow.com/questions/8106306/intent-bundle-returns-null-every-time\">intent-bundle-returns-null-every-time但没有正确的答案。

I know there are questions like: android-intent-bundle-always-null and intent-bundle-returns-null-every-time but there is no correct answer.

在我的活动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(活动2

In 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);
    ...
}

问题是,演员捆绑总是空。我已经调试它,并意图(意图= getIntent())的除了一个设置为空所有领域表示这是什么类( MapViewActivity )。

Problem is that extras bundle is always null. I have debugged it and Intent (intent = getIntent()) has all fields set to null except one indicating what class this is (MapViewActivity).

我也尝试通过把包intent.putExtras(B)具有相同的效果。
intent.putExtra(ASDF,真)仅用于调试的原因 - 我不能得到这个数据太(因为 getIntent()已设置为null,几乎所有领域)。

I've also tried putting the bundle via intent.putExtras(b) with the same effect. The intent.putExtra("asdf", true) is for debugging reasons only - I cannot get this data too (because getIntent() has almost all fields set to null).

推荐答案

我不知道什么是信息是,但我建议使数据的最基本的传球从一个活​​动到另一首涉及其他数据对象之前。

I am not sure what "Info" is for, but I suggest making the most basic passing of data from one activity to another first before involving other data objects.

活动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");

    }

这篇关于捆绑在意图设置后空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆