繁体   English   中英

第三活动的捆绑商品为空

[英]Bundle is null from third activity

您好,即时通讯使用bundle将字符串从我的MainActivity传递给我的第二个活动,该活动是子类型。 但是我在第三次活动中也需要相同的字符串。

我在主要活动中使用这样的代码

     Bundle getGenre_Bundle = new Bundle();
            if (genre.equals(selector.Crime)) {
             Intent i = new Intent(getBaseContext(),Crime.class);
                getGenre_Bundle.putString("crime",selector.Crime);
                i.putExtras(getGenre_Bundle);
                startActivity(i);

然后我在第二个活动中使用

 Bundle p = getIntent().getExtras();
    Genre = p.getString("crime");

这很好用,但是如果我尝试在第三个活动中调用它,它将在我的日志中返回一个错误,指出我的firebase child(Genre)cant be null

我已经通过在我的第二个活动中制作一个新的捆绑包来解决该问题,该捆绑包会重新收集字符串以传递给我的第三个活动。 但这样做的方式有点混乱。 有没有更简单/更好的方法来将字符串传递给任何活动?

您可以从第二个活动中的上一个活动中获取整个捆绑包,并只需使用putExtras()进行设置即可

Bundle old = getIntent().getExtras();
Intent thirdActivity = new Intent(this,thirdActivity.class);
thirdActivity.putExtras(p);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM