Bundle extra返回NULL(Bundle extra is returning NULL)

编程入门 行业动态 更新时间:2024-10-24 06:38:12
Bundle extra返回NULL(Bundle extra is returning NULL)

我遇到了问题。

在“A”类中我有代码:

Intent cInt = new Intent(Add_Product_Page.this, CategoryListActivity.class); Bundle extra = new Bundle(); extra.putBoolean("for_result", true); startActivityForResult(cInt, GET_CATEGORY, extra);

此代码来自另一个启动活动的类

Bundle extra = getIntent().getExtras(); if (extra != null) { isforResult = cInt.getBooleanExtra("for_result", true); setIsforResult(isforResult); } else { setIsforResult(false); } getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setTitle("Category"); }

我调试了A类 ,我得到了额外的值为true但是当我调试另一个类时,我在额外的时候得到NULL

有谁能够帮助我。

提前致谢

I'm having a problems.

In a class "A" I have the code:

Intent cInt = new Intent(Add_Product_Page.this, CategoryListActivity.class); Bundle extra = new Bundle(); extra.putBoolean("for_result", true); startActivityForResult(cInt, GET_CATEGORY, extra);

This code is from another class that starts the activity

Bundle extra = getIntent().getExtras(); if (extra != null) { isforResult = cInt.getBooleanExtra("for_result", true); setIsforResult(isforResult); } else { setIsforResult(false); } getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setTitle("Category"); }

I debugged Class A and I got the value of extra as true but when I am debugged another class I am getting NULL in the extra

Can anybody help me.

Thanks in advance

最满意答案

startActivityForResult()的第三个参数( Bundle option )不是“额外”包。 有关详细信息,请参阅Context#startActivity(Intent,Bundle) 。 那是启动配置。

使用Intent#putExtra(String, boolean)作为布尔额外内容。

Intent intent = new Intent(Add_Product_Page.this, CategoryListActivity.class); intent.putExtra("for_result", true); startActivityForResult(intent, GET_CATEGORY);

然后

boolean b = getIntent().getBooleanExtra("for_result", false);

这相当于

boolean b = getIntent().getExtras().getBoolean("for_result");

您还可以检查intent是否有额外的参数:

intent.hasExtra("for_result");

startActivityForResult()'s 3rd argument(Bundle option) is not "extra" bundles. See Context#startActivity(Intent, Bundle) for detail. That is launch configuration.

use Intent#putExtra(String, boolean) for boolean extras.

Intent intent = new Intent(Add_Product_Page.this, CategoryListActivity.class); intent.putExtra("for_result", true); startActivityForResult(intent, GET_CATEGORY);

then

boolean b = getIntent().getBooleanExtra("for_result", false);

This is equivalant to

boolean b = getIntent().getExtras().getBoolean("for_result");

Also you can check intent has extra parameter or not:

intent.hasExtra("for_result");

更多推荐

本文发布于:2023-07-22 12:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219937.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:extra   Bundle   returning   NULL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!