Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        10天前关闭。
                                                                                            
                
        
String signInMethod;
...

...
if (signInMethod != null) {
    if (signInMethod.equals("google")) {
        intent.putExtra("signInMethod", "google");
    } else if (signInMethod.equals("email")) {
        intent.putExtra("signInMethod", "email");
    }
} else {
    intent.putExtra("signInMethod", "alreadySignedIn");
}


java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference


如果不使用.equals方法,如何得到此错误?

java - 如果不使用.equals方法,如何得到此错误?-LMLPHP

java - 如果不使用.equals方法,如何得到此错误?-LMLPHP

java - 如果不使用.equals方法,如何得到此错误?-LMLPHP

最佳答案

用这个。

  String signInMethod = null;
    if(TextUtils.isEmpty(signInMethod)){
        Toast.makeText(context, "Some thing wrong please try again", Toast.LENGTH_SHORT).show();
    }else {
        if (signInMethod.equalsIgnoreCase("google")) {
            intent.putExtra("signInMethod", "google");
        } else if (signInMethod.equalsIgnoreCase("email")) {
            intent.putExtra("signInMethod", "email");
        } else {
            intent.putExtra("signInMethod", "alreadySignedIn");
        }
    }

07-26 05:57