跨活动传递信息时出现问题

发布于 2024-11-28 17:11:02 字数 681 浏览 0 评论 0 原文

我正在尝试传递以下信息:

Intent i=new Intent(ctx,SpendingsDetails.class);
extras.putString("SpendingAmount", "1");
extras.putString("SpendingDescription","2");
extras.putString("SpendingDate","3");
i.putExtras(extras);
startActivityForResult(i,1);

获取信息的活动执行以下信息。

    spendingAmount=(TextView)findViewById(R.id.spending_Sum);
spendingDetails=(TextView)findViewById(R.id.spending_Details);

Bundle extras=getIntent().getExtras();
if(extras!=null)
{
    spendingAmount.setText(extras.getString("SpendingAmount"));
    spendingDetails.setText(extras.getString("SpendingDescription"));
}

我收到错误消息,表明应用程序无法在第一个活动屏幕上继续。我做错了什么?

I am trying to pass this:

Intent i=new Intent(ctx,SpendingsDetails.class);
extras.putString("SpendingAmount", "1");
extras.putString("SpendingDescription","2");
extras.putString("SpendingDate","3");
i.putExtras(extras);
startActivityForResult(i,1);

The activity that gets the information does the following information..

    spendingAmount=(TextView)findViewById(R.id.spending_Sum);
spendingDetails=(TextView)findViewById(R.id.spending_Details);

Bundle extras=getIntent().getExtras();
if(extras!=null)
{
    spendingAmount.setText(extras.getString("SpendingAmount"));
    spendingDetails.setText(extras.getString("SpendingDescription"));
}

I am getting an error that the application cant continue on the first activities screen.What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

缺⑴份安定 2024-12-05 17:11:02

像这样使用

Intent i=new Intent(ctx,SpendingsDetails.class);
    i.putExtras("SpendingAmount", "1");
    i.putExtras("SpendingDescription","2");
    i.putExtras("SpendingDate","3");
    startActivityForResult(i,1);

Use like this

Intent i=new Intent(ctx,SpendingsDetails.class);
    i.putExtras("SpendingAmount", "1");
    i.putExtras("SpendingDescription","2");
    i.putExtras("SpendingDate","3");
    startActivityForResult(i,1);
羞稚 2024-12-05 17:11:02

您可以直接调用 getStringExtra:

spendingAmount.setText(getIntent().getStringExtra("SpendingAmount"));

依此类推以获得您需要的其他值。

当然,您也可以先检查该值是否存在:

String spending = getIntent().getStringExtra("SpendingAmount");
if(spending != null) {
  spendingAmount.setText(spending);
}

编辑:就像 Rasel 所说:

i.putExtra("SpendingAmount", "1");

我一开始没有看到这一点。

You could just call the getStringExtra directly:

spendingAmount.setText(getIntent().getStringExtra("SpendingAmount"));

And so on for the other values you need.

Of course you could also do a check to see if the value exists first:

String spending = getIntent().getStringExtra("SpendingAmount");
if(spending != null) {
  spendingAmount.setText(spending);
}

EDIT: Like Rasel said:

i.putExtra("SpendingAmount", "1");

I didn't see that at first.

看春风乍起 2024-12-05 17:11:02

我认为您声明了您的捆绑对象。但不分配内存。那就是你需要添加这段代码。

Intent i=new Intent(ctx,SpendingsDetails.class);
Bundle extras=new Bundle();// add this code and try once
extras.putString("SpendingAmount", "1");
extras.putString("SpendingDescription","2");
extras.putString("SpendingDate","3");
i.putExtras(extras);
startActivityForResult(i,1);

希望有帮助。

I think you declared your bundle object. But does not allocate the memory. that is you need to add this code.

Intent i=new Intent(ctx,SpendingsDetails.class);
Bundle extras=new Bundle();// add this code and try once
extras.putString("SpendingAmount", "1");
extras.putString("SpendingDescription","2");
extras.putString("SpendingDate","3");
i.putExtras(extras);
startActivityForResult(i,1);

Hope it helps.

掩耳倾听 2024-12-05 17:11:02

你在哪里声明了额外费用?看看变化。

Intent i=new Intent(ctx,SpendingsDetails.class);
Bundle extras=new Bundle();
extras.putString("SpendingAmount", "1");
extras.putString("SpendingDescription","2");
extras.putString("SpendingDate","3");
i.putExtras(extras);
startActivityForResult(i,1);

Where did you declare extras? Look at the change.

Intent i=new Intent(ctx,SpendingsDetails.class);
Bundle extras=new Bundle();
extras.putString("SpendingAmount", "1");
extras.putString("SpendingDescription","2");
extras.putString("SpendingDate","3");
i.putExtras(extras);
startActivityForResult(i,1);
盛装女皇 2024-12-05 17:11:02

请查看 LogCat 以获取异常堆栈跟踪。这是当事情“不起作用”时首先要查看的地方。

Please look into the LogCat for exception stack traces. Thats the first place to look when things "don't work".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文