Android:在活动之间传递数据 - 多个intent.putExtra不起作用?
在活动之间传递数据时遇到一个非常烦人的问题。
这是我用来成功将进度条的值传递给不同活动的代码:
public void WhenClicked(View view)
{
view.clearAnimation();
Intent intent = new Intent("com.android.Test.QUESTION");
if (progressBar != null)
{
if (progressBar.getProgress() != 0)
{
intent.putExtra("ProgressBarValue", progressBar.getProgress());
}
}
startActivity(intent);
}
好的,这样就成功了。现在,当我把它改成这样时,它爆炸了:
public void WhenClicked(View view, String category)
{
view.clearAnimation();
Intent intent = new Intent("com.android.Test.QUESTION");
intent.putExtra("Category", category);
if (progressBar != null)
{
if (progressBar.getProgress() != 0)
{
intent.putExtra("ProgressBarValue", progressBar.getProgress());
}
}
startActivity(intent);
}
我不明白问题是什么。我什至尝试将其全部放入一个捆绑包中,并将该捆绑包作为额外的内容添加 - 这也使其崩溃。也许我很愚蠢,而且我只是盯着我的代码太久了,但任何帮助都会很棒!
这是我第一次使用 Android,这让我很沮丧!
预先感谢各位!
Having a very annoying problem with passing data between activities.
This is the code I use to successfully pass the value of a progress bar to a different activity:
public void WhenClicked(View view)
{
view.clearAnimation();
Intent intent = new Intent("com.android.Test.QUESTION");
if (progressBar != null)
{
if (progressBar.getProgress() != 0)
{
intent.putExtra("ProgressBarValue", progressBar.getProgress());
}
}
startActivity(intent);
}
Okay, so that worked. Now, when I change it to this, it blows up:
public void WhenClicked(View view, String category)
{
view.clearAnimation();
Intent intent = new Intent("com.android.Test.QUESTION");
intent.putExtra("Category", category);
if (progressBar != null)
{
if (progressBar.getProgress() != 0)
{
intent.putExtra("ProgressBarValue", progressBar.getProgress());
}
}
startActivity(intent);
}
I don't understand what the problem is. I've even tried sticking it all into a bundle and adding the bundle as an extra - that just made it crash as well. Maybe I'm being stupid and I've just been staring at my code too long, but any help would be great!
This is my first time with Android and it's killing me!
Thanks in advance guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您需要创建
bundle 对象(Bundle bnd=new Bundle();)
和接下来的bnd.putString("param1", "test");
接下来创建意图:
在第二个活动中,您需要获取捆绑包值,例如:
first you need to create
bundle object(Bundle bnd=new Bundle();)
and nextbnd.putString("param1", "test");
next create intent:
In 2nd activity u need to get bundel value like :
假设您有一个名为 QUESTION 的活动,您可能会尝试将 .class 放在末尾,并将“this”作为第一个参数:
如果您没有 QUESTION 活动,那么这是另一个问题。我假设您的活动都在同一个应用程序中?
Assuming that you have an activity called QUESTION, you might try to put .class on the end as well as "this" for first param:
if you don't have a QUESTION activity then that is another problem. I'm assuming your activities are in the same app?
我相信,应该处理此意图操作
com.android.Test.QUESTION
的活动无法理解您的类别,即intent.putExtra("Category",category);
。您可以尝试通过两种方式修复它:
Intent Intent = new Intent("youCurrentClass","theClassYouWantToCall");
而无需提供额外的内容类别,这将启动指定的活动。在显式 Intent 的情况下,Android 系统不会与 Intent 过滤器进行比较来匹配 Intent 对象。希望这有帮助,
存货单元
I believe, the activity which should handle this intent action
com.android.Test.QUESTION
does not understand your category i.eintent.putExtra("Category", category);
.You can try fixing it in 2 ways:
Intent intent = new Intent("youCurrentClass","theClassYouWantToCall");
without feeding additional category, this will launch the specified activity. In case of explicit Intent Android system does not do a comparison with the intent-filters to match Intent Object.Hope this helps,
sku