Android:在活动之间传递数据 - 多个intent.putExtra不起作用?

发布于 2024-12-03 18:29:51 字数 1062 浏览 1 评论 0原文

在活动之间传递数据时遇到一个非常烦人的问题。

这是我用来成功将进度条的值传递给不同活动的代码:

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 技术交流群。

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

发布评论

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

评论(3

行雁书 2024-12-10 18:29:51

首先,您需要创建 bundle 对象(Bundle bnd=new Bundle();) 和接下来的 bnd.putString("param1", "test");
接下来创建意图:

Intent myIntent = new Intent(current classname.this,nextactivity.class);                    
myIntent.putExtras(bnd);
startActivityForResult(myIntent, 0);

在第二个活动中,您需要获取捆绑包值,例如:

 Bundle bundle = this.getIntent().getExtras();
 String _getData=bundle.getString("param1");

first you need to create bundle object(Bundle bnd=new Bundle();) and next bnd.putString("param1", "test");
next create intent:

Intent myIntent = new Intent(current classname.this,nextactivity.class);                    
myIntent.putExtras(bnd);
startActivityForResult(myIntent, 0);

In 2nd activity u need to get bundel value like :

 Bundle bundle = this.getIntent().getExtras();
 String _getData=bundle.getString("param1");
鸩远一方 2024-12-10 18:29:51

假设您有一个名为 QUESTION 的活动,您可能会尝试将 .class 放在末尾,并将“this”作为第一个参数:

Intent intent = new Intent(this, QUESTION.class);

如果您没有 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:

Intent intent = new Intent(this, QUESTION.class);

if you don't have a QUESTION activity then that is another problem. I'm assuming your activities are in the same app?

九厘米的零° 2024-12-10 18:29:51

我相信,应该处理此意图操作 com.android.Test.QUESTION 的活动无法理解您的类别,即 intent.putExtra("Category",category);

您可以尝试通过两种方式修复它:

  1. 如果接收活动来自您自己的应用程序,则尝试使用显式意图,即 Intent Intent = new Intent("youCurrentClass","theClassYouWantToCall"); 而无需提供额外的内容类别,这将启动指定的活动。在显式 Intent 的情况下,Android 系统不会与 Intent 过滤器进行比较来匹配 Intent 对象。
  2. 更改与意图接收活动相关的类别部分。

希望这有帮助,
存货单元

I believe, the activity which should handle this intent action com.android.Test.QUESTION does not understand your category i.e intent.putExtra("Category", category);.

You can try fixing it in 2 ways:

  1. If the receiving activity is from your own application, then try using explicit intent i.e 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.
  2. Change the category section in relation to intent receiving Activity.

Hope this helps,
sku

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