Hello stackoverflow世界!有一段时间,您正在寻找的答案尚未在此处,因此您不得不提出要求。
我的问题是我使用if语句检查空值,但我仍然得到NullPointerException。
我的某个应用已对我的应用进行了测试,并且遇到了NullPointerException,但我无法为我的生活重新创建该错误。使问题更难确定。
这是应用程序的开发控制台上的CrashReport:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dev.admin.blacklist/com.dev.admin.blacklist.Page_RecentCalls}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2391)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5387)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:831)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.dev.admin.blacklist.Page_RecentCalls.getRecentContacts(Page_RecentCalls.java:110)
at com.dev.admin.blacklist.Page_RecentCalls.onCreate(Page_RecentCalls.java:32)
at android.app.Activity.performCreate(Activity.java:5363)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2335)
... 11 more
这是抛出此错误的第110行:
Cursor cursor = getContentResolver().query(queryUri, projection, null, null, sortOrder);
while (cursor.moveToNext())
{
String phoneNumber = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
String title = (cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)));
if(phoneNumber != null && !phoneNumber.isEmpty() && !phoneNumber.equals(manager.getVoiceMailNumber().toString()))
{
if(title == null || title.isEmpty())title = "No-name";
if(recentcontacts.size() > 1 )
{
String one = "";
one = recentcontacts.get(recentcontacts.size() - 1).getFirst(); //Line 110 error was here
if(!one.equals(title)) recentcontacts.add(new Object_TwoString(title,phoneNumber));
}
else recentcontacts.add(new Object_TwoString(title,phoneNumber));
}
}
cursor.close();
return recentcontacts;
}
}
public Object_TwoString(String one, String two)
{
setStrings(one,two);
}
public String[] getStrings(){ String[] strings = {firstString,secondString}; return strings;}
public String getFirst(){return firstString;}
public String getSecond(){return secondString;}
public Boolean IsContained(List<Object_TwoString> obj)
{
for(Object_TwoString ts: obj)
{
if(ts.getFirst().equals(firstString) && ts.getSecond().equals(secondString))return true;
}
return false;
}
答案 0 :(得分:-1)
实际发生NullPointerException:
getContentResolver()
调用该方法会返回null然后调用 query(),因为您无法在 null 对象上调用方法。 (FWIW: null 不应该存在,并且是所有软件工程项目的癌症)
我强烈怀疑 getContentResolver()(遗憾的是,类型不强制执行)所需的某些初始化步骤正在咬你。有一种时间耦合要求你做一些雨舞,以使该方法正常工作,这是一个糟糕的设计选择。