如何检查是否有按意图发送的捆绑包?

发布于 2024-10-20 04:14:13 字数 752 浏览 4 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(1

╰ゝ天使的微笑 2024-10-27 04:14:13

从您的 MainActivity 中,您可以通过 startActivityForResult 启动子GameActivity,一旦完成,您就可以通过 接收游戏结果onActivityResult

像这样的东西:

MainActivity:

private void startGameActivity() {
    Intent i = new Intent(getApplicationContext(), GameActivity.class);
    i.putExtra("some.key.here", "value");
    startActivityForResult(i, 0);
}

@Override protected void onActivityResult( int req, int resp, Intent data ) {
    super.onActivityResult(req, resp, data);
    // process your received "data" from GameActivity ...
}

GameActivity:

public void onCreate( Bundle savedInstanceState ) {
    // ...
    Bundle b = getIntent().getExtras();
    // ... process your extras from MainActivity
}

public void finishMySubActivity() {
    Intent data = new Intent();
    data.putExtra("some.other.key", GameResultsHere);
    setResult(RESULT_OK, data);
    finish();
 }

From your MainActivity you could start sub-GameActivity via startActivityForResult and once it is finished, then you could receive your game results back via onActivityResult.

Something like this:

MainActivity:

private void startGameActivity() {
    Intent i = new Intent(getApplicationContext(), GameActivity.class);
    i.putExtra("some.key.here", "value");
    startActivityForResult(i, 0);
}

@Override protected void onActivityResult( int req, int resp, Intent data ) {
    super.onActivityResult(req, resp, data);
    // process your received "data" from GameActivity ...
}

GameActivity:

public void onCreate( Bundle savedInstanceState ) {
    // ...
    Bundle b = getIntent().getExtras();
    // ... process your extras from MainActivity
}

public void finishMySubActivity() {
    Intent data = new Intent();
    data.putExtra("some.other.key", GameResultsHere);
    setResult(RESULT_OK, data);
    finish();
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文