简体   繁体   English

第三活动的捆绑商品为空

[英]Bundle is null from third activity

hi im using bundle to pass a string from my MainActivity to my second activity which is sub genre. 您好,即时通讯使用bundle将字符串从我的MainActivity传递给我的第二个活动,该活动是子类型。 but i also need the same string in my third activity. 但是我在第三次活动中也需要相同的字符串。

i use my code like this in my main acitvity 我在主要活动中使用这样的代码

     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);

then i call it in my second Activity using 然后我在第二个活动中使用

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

this works great but if i try to call it on my third activity it returns an error in my log that my firebase child(Genre)cant be null . 这很好用,但是如果我尝试在第三个活动中调用它,它将在我的日志中返回一个错误,指出我的firebase child(Genre)cant be null

Ive fixed the problem by making a new bundle in my second activity that recollects the String to pass to my third activity. 我已经通过在我的第二个活动中制作一个新的捆绑包来解决该问题,该捆绑包会重新收集字符串以传递给我的第三个活动。 but it seams a bit of a messy way of doing it. 但这样做的方式有点混乱。 is there an easier/better way to pass strings to any activity? 有没有更简单/更好的方法来将字符串传递给任何活动?

You can fetch entire bundle from your previous activity in second activity and simply set in intent with putExtras() 您可以从第二个活动中的上一个活动中获取整个捆绑包,并只需使用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