Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中处理空值的更好方法?_Android_Software Quality - Fatal编程技术网

在android中处理空值的更好方法?

在android中处理空值的更好方法?,android,software-quality,Android,Software Quality,在android中,每当我需要从其他活动/意图等获取数据时,我的代码基本上必须遵循以下逻辑: Intent intent = getIntent(); if (intent != null) { String intentAction = intent.getAction(); if (intentAction != null && intentAction.equals("whatever")) { // Do some stuff now tha

在android中,每当我需要从其他活动/意图等获取数据时,我的代码基本上必须遵循以下逻辑:

Intent intent = getIntent();

if (intent != null) {
    String intentAction = intent.getAction();

    if (intentAction != null && intentAction.equals("whatever")) {

    // Do some stuff now that we know there are now  null values
    }
}
在我看来,非常冗长和嵌套的代码,每次我这么做的时候,我对自己说“一定有更好的方法”

有吗?那会是什么


错误的做法,但较短(且无if):


您可以将这些
if语句
组合成一个。如果
&
前面的第一个参数返回
false,代码将“退出”,因此不会出现错误

Intent intent = getIntent();

if (intent != null && intent.getAction() != null && intent.getAction().equals("whatever") {

    // Do some stuff now that we know there are now  null values

}
多亏@Tomkarho的建议,这里还有一条更短的路

Intent intent = getIntent();

if (intent != null && "whatever".equals(intent.getAction()) {

    // Do some stuff now that we know there are now  null values

}

我个人会创建一些助手方法来清理主逻辑。如果这是经常出现的代码,您可以创建一个基本的
活动
类来保存
getIntentAction
方法,或者在助手中创建一个静态方法,将
活动
或其
意图
作为参数

对于字符串比较,可以使用
TextUtils.equals()
。或者,如果您有一个包含操作名称的字符串,则可以将其用作
equals
方法的左侧。只是要确保以后不要交换订单

一些示例代码:

public static final String WhateverAction = "whatever";

public String getIntentAction()
{
    Intent intent = getIntent();
    return intent == null ? null : intent.getAction();
}
使用左侧的比较字符串:

public void processMyIntent()
{
    String action = getIntentAction();
    if(WhateverAction.equals(action))
    {
        // do something
    }
    else if("thisAlsoWorksAction".equals(action)
    {
        // do something else
    }
    else
    {
        // got null or unexpected value
    }
}
使用
TextUtils

public void processMyIntentTextUtils()
{
    String action = getIntentAction();
    if(TextUtils.equals(action, WhateverAction))
    {
        // do something
    }
    if(TextUtils.equals(action, "anotherAction"))
    {
        // do something else
    }
    else
    {
        // got null or unexpected value
    }
}
使用开关:

public void processMyIntentSwitch()
{
    String action = getIntentAction();
    switch(action)
    {
        case WhateverAction:
           //...
           break;

        default:
           // got null or unexpected value
    }
}
您也可以不使用
getIntentAction
方法,只使用一行代码,尽管有点冗长:

String intentAction = getIntent() != null ? getIntent().getAction() : null;

如果选中
getIntent().getExtras()!=空
?可以对空指针例外使用try-catch块如果多次使用同一个代码段,为什么不将其设置为静态?因此java/android中没有现成的抽象来实现这一点?这就是我问题的重点。当然,我可以创建帮助器类/方法等。如果必要的话,但我本以为这样的问题已经在更深层次上得到了解决。用try-catch包装所有内容肯定不是一个好做法。如果intent.getAction()为null怎么办?@Mark你的解决方案不能用TextUtils进一步简化吗?如果(intent!=null&&TextUtils.equals(intent.getAction(),“which”)@Tomkarho,您是对的!聪明。我已经更新了我的答案,加入了另一种方式。
String intentAction = getIntent() != null ? getIntent().getAction() : null;