English 中文(简体)
额外款项的确定或不适用
原标题:Check if extras are set or not

在开始一项活动时是否通过额外措施?

我想做的是(关于“活动”的<编码>。

    Bundle extras = getIntent().getExtras();
    String extraStr = extras.getString("extra");

    if (extraStr == null) {
        extraStr = "extra not set";
    }

但是,这正在扔下一个<代码>java.lang.NullPointerException。

谢谢。

最佳回答
问题回答

同样,我也存在西语问题。 在我的案件中,如果我检查了bundle.getString()等于无效,就会出现一个无效的例外。

http://www.hchr.org。

Intent intent = getIntent();        
    if(intent.hasExtra("nomeUsuario")){
        bd = getIntent().getExtras();
        if(!bd.getString("nomeUsuario").equals(null)){
            nomeUsuario = bd.getString("nomeUsuario");
        }
    }
if (this.getIntent().getExtras() != null && this.getIntent().getExtras().containsKey("yourKey")) {
   // intent is not null and your key is not null
}

我认为,你需要检查什么时候? 无

Bundle extras = getIntent().getExtras();
   if (extras != null) {
        String extraStr = extras.getString("extra");
    }else {
        extraStr = "extra not set";
    }

我将在你的情况下使用这一解决办法。

String extraStr;
    try {
        extraStr = getIntent().getExtras().getString("extra");
    } catch (NullPointerException e ) {
        extraStr = "something_else";
    }

<>《工作守则>>

如果你想要检查第一病人,就没有额外的

 Intent intent = getIntent();
        if (intent.getExtras() == null){
            startActivity(new Intent(Splash.this, Main.class));
            overridePendingTransition(R.anim.enter, R.anim.exit);
            finish();
        }else {
            if (intent.hasExtra("type")) {
                String type = getIntent().getStringExtra("type");

                switch (type){
                    case "showRateUsDialog":
                        Intent i = new Intent(Splash.this, Main.class);
                        i.putExtra("type", "showRateUsDialog");
                        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(i);
                        overridePendingTransition(R.anim.enter, R.anim.exit);
                        finish();
                        break;
                    case "refer":
                        Intent i2 = new Intent(Splash.this, Refer.class);
                        i2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(i2);
                        overridePendingTransition(R.anim.enter, R.anim.exit);
                        finish();
                        break;
                    default:
                        startActivity(new Intent(Splash.this, Main.class));
                        overridePendingTransition(R.anim.enter, R.anim.exit);
                        finish();
                }
            }
        }
    }




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...