开发者

NullPointerException from getIntent().getExtras().getString("to")

开发者 https://www.devze.com 2023-02-22 23:35 出处:网络
InboxDetailActivity.java: Intent i = new Intent(InboxDetailActivity.this,Compose.class); Bundle b = new Bundle();

InboxDetailActivity.java:

Intent i = new Intent(InboxDetailActivity.this,Compose.class);
Bundle b = new Bundle();
b.putString("To", ConstantData.inbox_from);
Log.d("From Value", ConstantData.inbox_from);
b.putString("Subject", "RE:" + ConstantData.inbox_subject);
Log.d("Subject Value", ConstantData.inbox_subject);
b.putString("FromId", ConstantData.inbox_fromid开发者_运维技巧);
Log.d("From Id Value",ConstantData.inbox_fromid);
i.putExtras(b);
startActivity(i);

Compose.java:

Intent i = getIntent();
Bundle b = i.getExtras();

to = b.getString("To");
subject = b.getString("Subject");
toId = b.getString("FromId");

I am getting NullPointerException at to = b.getString("To");


Bundle b = i.getExtras();

getExtras() returns null.


Agree with John's answer adding possible solution.

What you are doing is create a bundle , insert values in this and then pass this bundle. And than you just fetch all the values one by one using its keys.

I am working with bundles but I simply add desired values directly using putExtra method. And I haven't got any problem till date. I recommend you to use put extra and check whether it works or not.

I would like to know what makes you to apply this way for bundles? Have you just read it somewhere and started applying this method ? or you have got some options and after some consideration you found it better to apply this method OR your requirement states that. Because normally me and my peers doesn't use bundles and directly pass the extras. And that works for me every time.


using this instead of bundle

        i.putString("To", ConstantData.inbox_from);
        Log.d("From Value", ConstantData.inbox_from);
        i.putString("Subject", "RE:" + ConstantData.inbox_subject);
        Log.d("Subject Value", ConstantData.inbox_subject);
        i.putString("FromId", ConstantData.inbox_fromid);
        Log.d("From Id Value",ConstantData.inbox_fromid);

and in another class..

        to = getIntent().getString("To"); 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号