简体   繁体   English

Android文本字符串比较

[英]Android text string comparison

In a custom SimpleCursorAdapter, I'm trying to compare a status String, with confusing results. 在自定义的SimpleCursorAdapter中,我试图将状态String与令人困惑的结果进行比较。

My string is initialised from the cursor like this (and I've checked with toast that it contains the expected values). 我的字符串是这样从游标初始化的(并且我已经向toast检查它是否包含期望值)。

String visitStatus = cursor.getString(cursor.getColumnIndex(CallData.COLUMN_VisitStatus));

visitStatus can be null, Open, Cancelled or Complete. visitStatus可以为null,Open,Cancel或Complete。

If I try to compare visitStatus to "any string in quotes", the app crashes with a NullPointerException. 如果我尝试将visitStatus与“带引号的任何字符串”进行比较,则该应用程序将崩溃,并显示NullPointerException。 Only if I compare to null do I get anything at all - and that is no use to me 仅当我比较null时,我什么都不会得到-对我没有用

if(visitStatus.equals(null)) // the app crashes with a NullPointerException
if(visitStatus == null) // doesn't crash
if(visitStatus != null) // doesn't crash
if(visitStatus == "Complete") // doesn't crash or do anything
if(visitStatus.equals("Complete")) // the app crashes with a NullPointerException.

Basically, I can compare to null, but only in the way that isn't supposed to work. 基本上,我可以将其与null进行比较,但只能以不起作用的方式进行比较。 I can't compare to actual strings such as "Open" or "Complete". 我无法与“ Open”或“ Complete”之类的实际字符串进行比较。

I'm going slightly nuts with this, and am badly missing my C# comfort zone. 我对此有点发疯,并且非常想念我的C#舒适区。 This particular activity is a nightmare of listfragments, contentproviders, customadapters, viewpagers, pagertitlestrips and list row xml templates! 此特定活动是列表片段,内容提供者,自定义适配器,viewpagers,pagertitlestrip和列表行xml模板的噩梦!

halp! hal!

This is because visitStatus is null . 这是因为visitStatusnull Whenever you try to access its methods, it crashes. 每当您尝试访问其方法时,它就会崩溃。 ( That is: visitString.equals() , visitString.length() , etc., all will crash. ) 也就是说: visitString.equals()visitString.length()等都会崩溃。

However, the equality operator ( == ) supports null parameters on either side of it. 但是,相等运算符( == )在其任一侧都支持null参数。 (So, if (null == null) is a valid check.) (因此, if (null == null)是有效的检查。)

You should check like this: 您应该像这样检查:

if (visitStatus != null && visitStatus.equals("Complete")) {
    // ...
}

Or, you can do "Yoda syntax" (backwards checking), which supports null parameters: 或者,您可以执行“ Yoda语法”(向后检查),该语法支持null参数:

if ("Complete".equals(visitStatus)) {
    // ...
}

Also, a final note: You cannot compare string contents using == (as in, you cannot do "a" == new String("a") , nor visitString == "Complete" ). 另外,最后一点要注意:您不能使用==比较字符串内容(例如,您不能执行"a" == new String("a")visitString == "Complete" )。 For a detailed explanation on that, see this Q&A thread . 有关此内容的详细说明,请参见此Q&A主题

String should be compared using .equals() 应该使用.equals()比较String

The NullPointerException caused because the visitStatus is null 由于visitStatusnull导致NullPointerException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM