如何检查是否有按意图发送的捆绑包?
我有一个 Android 应用程序,其中有两个活动。启动 Activity 是用户选择类别的地方,第二个 Activity 是用户玩游戏并获得结果的地方。然后,该结果被传回要在 Facebook 上发布的第一个活动。
为了在活动之间传递数据,我使用以下代码:
Bundle extras = new Bundle();
extras.putInt("categoryid", categoryid);
Intent i = new Intent(MenuView.this, CreateTestView.class);
i.putExtras(extras);
startActivity(i);
这是双向的。现在我的问题是: 第一次启动 MenuActivity 时,没有传递任何包,因此当我尝试检索额外内容时,出现空指针异常。如何在启动时检查是否有捆绑包通过?
我尝试了这种方式:
Bundle b = this.getIntent().getExtras();
if(b==null){}
else{
noqs = b.getInt("noqs");
point = b.getInt("point");
但是每次都会出现 b==null,即使在完成游戏并且捆绑包是从 GameActivity 发送之后也是如此。
I have an Android application in which there are two activities. The start-up activity is where the user chooses category, and the second activity is where the user plays a game and gets a result. This result is then passed back to the first activity to be posted on facebook.
To pass data between activities I use this code:
Bundle extras = new Bundle();
extras.putInt("categoryid", categoryid);
Intent i = new Intent(MenuView.this, CreateTestView.class);
i.putExtras(extras);
startActivity(i);
This goes both ways. Now to my problem:
The first time I start the MenuActivity there is no bundle being passed, and therefore I get a nullpointer exception when I try to retreive the extras. How can I use a check at start-up to see if there is a bundle beeing passed or not?
I tried it this way:
Bundle b = this.getIntent().getExtras();
if(b==null){}
else{
noqs = b.getInt("noqs");
point = b.getInt("point");
But this goes as b==null every time, even after finished game and the bundle is sent from the GameActivity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的
MainActivity
中,您可以通过startActivityForResult
启动子GameActivity
,一旦完成,您就可以通过接收游戏结果onActivityResult
。像这样的东西:
MainActivity:
GameActivity:
From your
MainActivity
you could start sub-GameActivity
viastartActivityForResult
and once it is finished, then you could receive your game results back viaonActivityResult
.Something like this:
MainActivity:
GameActivity: