我该如何判断Android中是否存在意图附加功能?

时间:2022-01-20 07:17:33

I have this code that checks for a value of an extra in an Intent on an Activity that is called from many places in my app:

我有这段代码,在我的应用程序中很多地方调用的活动中检查一个额外的值:

getIntent().getExtras().getBoolean("isNewItem")

If isNewItem isn't set, will my code crash? Is there any way to tell if it's been set or not before I call it?

如果isNewItem没有设置,我的代码会崩溃吗?有没有办法在我调用它之前判断它是否被设置?

What is the proper way to handle this?

如何处理这个问题?

5 个解决方案

#1


94  

As others have said, both getIntent() and getExtras() may return null. Because of this, you don't want to chain the calls together, otherwise you might end up calling null.getBoolean("isNewItem"); which will throw a NullPointerException and cause your application to crash.

正如其他人所说,getIntent()和getExtras()都可能返回null。因此,您不希望将调用链接在一起,否则您可能最终调用null.getBoolean(“isNewItem”);这将抛出NullPointerException,并导致应用程序崩溃。

Here's how I would accomplish this. I think it's formatted in the nicest way and is very easily understood by someone else who might be reading your code.

下面是我如何做到这一点。我认为它的格式是最好的,并且很容易被其他人理解,他们可能正在阅读您的代码。

// You can be pretty confident that the intent will not be null here.
Intent intent = getIntent();

// Get the extras (if there are any)
Bundle extras = intent.getExtras();
if (extras != null) {
    if (extras.containsKey("isNewItem")) {
        boolean isNew = extras.getBoolean("isNewItem", false);

        // TODO: Do something with the value of isNew.
    }
}

You don't actually need the call to containsKey("isNewItem") as getBoolean("isNewItem", false) will return false if the extra does not exist. You could condense the above to something like this:

实际上不需要调用containsKey(“isNewItem”),因为如果不存在额外的属性,getBoolean(“isNewItem”,false)将返回false。你可以把上面压缩成这样:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    boolean isNew = extras.getBoolean("isNewItem", false);
    if (isNew) {
        // Do something
    } else {
        // Do something else
    }
}

You can also use the Intent methods to access your extras directly. This is probably the cleanest way to do so:

您也可以使用意图方法直接访问您的附加文件。这可能是最干净的方式:

boolean isNew = getIntent().getBooleanExtra("isNewItem", false);

Really any of the methods here are acceptable. Pick one that makes sense to you and do it that way.

这里的任何方法都是可以接受的。选择一个对你有意义的,这样做。

#2


7  

The problem is not the getBoolean() but the getIntent().getExtras()

问题不在于getBoolean(),而在于getIntent().getExtras()

Test this way:

测试:

if(getIntent() != null && getIntent().getExtras() != null)
  myBoolean = getIntent().getExtras().getBoolean("isNewItem");

by the way, if isNewItem doesn't exist, it returns de default vaule false.

顺便说一下,如果isNewItem不存在,它将返回默认的vaule false。

Regards.

的问候。

#3


3  

You can do this:

你可以这样做:

Intent intent = getIntent();
if(intent.hasExtra("isNewItem")) {
   intent.getExtras().getBoolean("isNewItem");
}

#4


1  

getIntent() will return null if there is no Intent so use...

如果没有意图,getIntent()将返回null,因此使用…

boolean isNewItem = false;
Intent i = getIntent();
if (i != null)
    isNewItem = i.getBooleanExtra("isNewItem", false);

#5


0  

It will not crash unless until you use it! You don't have to get it if it exists but if you try, for some reason, to use a an "extra" which doesn' exists your system will crash.

它不会崩溃,除非你使用它!如果它存在,你不需要得到它,但是如果你尝试,出于某种原因,使用一个“额外的”它不存在,你的系统就会崩溃。

So, try o do something like:

所以,试着做一些像:

final Bundle bundle = getIntent().getExtras();

boolean myBool=false;

if(bundle != null) {
    myBool = bundle.getBoolean("isNewItem");
}

This way you make sure your app won't crash. (and make sure you have a valid Intent :))

这样你可以确保你的应用不会崩溃。(并确保你有一个有效的意图:)

#1


94  

As others have said, both getIntent() and getExtras() may return null. Because of this, you don't want to chain the calls together, otherwise you might end up calling null.getBoolean("isNewItem"); which will throw a NullPointerException and cause your application to crash.

正如其他人所说,getIntent()和getExtras()都可能返回null。因此,您不希望将调用链接在一起,否则您可能最终调用null.getBoolean(“isNewItem”);这将抛出NullPointerException,并导致应用程序崩溃。

Here's how I would accomplish this. I think it's formatted in the nicest way and is very easily understood by someone else who might be reading your code.

下面是我如何做到这一点。我认为它的格式是最好的,并且很容易被其他人理解,他们可能正在阅读您的代码。

// You can be pretty confident that the intent will not be null here.
Intent intent = getIntent();

// Get the extras (if there are any)
Bundle extras = intent.getExtras();
if (extras != null) {
    if (extras.containsKey("isNewItem")) {
        boolean isNew = extras.getBoolean("isNewItem", false);

        // TODO: Do something with the value of isNew.
    }
}

You don't actually need the call to containsKey("isNewItem") as getBoolean("isNewItem", false) will return false if the extra does not exist. You could condense the above to something like this:

实际上不需要调用containsKey(“isNewItem”),因为如果不存在额外的属性,getBoolean(“isNewItem”,false)将返回false。你可以把上面压缩成这样:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    boolean isNew = extras.getBoolean("isNewItem", false);
    if (isNew) {
        // Do something
    } else {
        // Do something else
    }
}

You can also use the Intent methods to access your extras directly. This is probably the cleanest way to do so:

您也可以使用意图方法直接访问您的附加文件。这可能是最干净的方式:

boolean isNew = getIntent().getBooleanExtra("isNewItem", false);

Really any of the methods here are acceptable. Pick one that makes sense to you and do it that way.

这里的任何方法都是可以接受的。选择一个对你有意义的,这样做。

#2


7  

The problem is not the getBoolean() but the getIntent().getExtras()

问题不在于getBoolean(),而在于getIntent().getExtras()

Test this way:

测试:

if(getIntent() != null && getIntent().getExtras() != null)
  myBoolean = getIntent().getExtras().getBoolean("isNewItem");

by the way, if isNewItem doesn't exist, it returns de default vaule false.

顺便说一下,如果isNewItem不存在,它将返回默认的vaule false。

Regards.

的问候。

#3


3  

You can do this:

你可以这样做:

Intent intent = getIntent();
if(intent.hasExtra("isNewItem")) {
   intent.getExtras().getBoolean("isNewItem");
}

#4


1  

getIntent() will return null if there is no Intent so use...

如果没有意图,getIntent()将返回null,因此使用…

boolean isNewItem = false;
Intent i = getIntent();
if (i != null)
    isNewItem = i.getBooleanExtra("isNewItem", false);

#5


0  

It will not crash unless until you use it! You don't have to get it if it exists but if you try, for some reason, to use a an "extra" which doesn' exists your system will crash.

它不会崩溃,除非你使用它!如果它存在,你不需要得到它,但是如果你尝试,出于某种原因,使用一个“额外的”它不存在,你的系统就会崩溃。

So, try o do something like:

所以,试着做一些像:

final Bundle bundle = getIntent().getExtras();

boolean myBool=false;

if(bundle != null) {
    myBool = bundle.getBoolean("isNewItem");
}

This way you make sure your app won't crash. (and make sure you have a valid Intent :))

这样你可以确保你的应用不会崩溃。(并确保你有一个有效的意图:)