我正在开发一个显示对话列表的 android 应用程序。
我的代码在三星 Galaxy S、Nexus one 和 Galaxy Nexus 中运行良好,但查询在我的 Galaxy Grand 手机中出现问题。
这是我的代码:
public ArrayList<String> getSMS()
{
ArrayList<String> sms = new ArrayList<String>();
Uri uriSMSURI = Uri.parse("content://mms-sms/conversations/");
Cursor cursor = getContentResolver().query(uriSMSURI, null, null, null, "date desc");
//cursor.moveToFirst();
while (cursor.moveToNext())
{
String address = cursor.getString(cursor.getColumnIndex("address"));
String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
String read = cursor.getString(cursor.getColumnIndexOrThrow("read"));
//to fetch the contact name of the conversation
String contactName = address;
Uri Nameuri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
Cursor cs= getContentResolver().query(Nameuri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+address+"'",null,null);
if(cs.getCount()>0)
{
cs.moveToFirst();
contactName = cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
sms.add(contactName + "\n"+body);
}
return sms;
}
以下是堆栈跟踪:
06-24 17:26:21.744: E/AndroidRuntime(12764): FATAL EXCEPTION: main
06-24 17:26:21.744: E/AndroidRuntime(12764): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ssn.sms/com.ssn.sms.test}: java.lang.NullPointerException
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.ActivityThread.access$700(ActivityThread.java:140)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.os.Looper.loop(Looper.java:137)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.ActivityThread.main(ActivityThread.java:4935)
06-24 17:26:21.744: E/AndroidRuntime(12764): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 17:26:21.744: E/AndroidRuntime(12764): at java.lang.reflect.Method.invoke(Method.java:511)
06-24 17:26:21.744: E/AndroidRuntime(12764): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-24 17:26:21.744: E/AndroidRuntime(12764): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-24 17:26:21.744: E/AndroidRuntime(12764): at dalvik.system.NativeStart.main(Native Method)
06-24 17:26:21.744: E/AndroidRuntime(12764): Caused by: java.lang.NullPointerException
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.os.Parcel.readException(Parcel.java:1431)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.content.ContentResolver.query(ContentResolver.java:372)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.content.ContentResolver.query(ContentResolver.java:315)
06-24 17:26:21.744: E/AndroidRuntime(12764): at com.ssn.sms.test.getSMS(test.java:157)
06-24 17:26:21.744: E/AndroidRuntime(12764): at com.ssn.sms.test.onCreate(test.java:74)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.Activity.performCreate(Activity.java:5206)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
06-24 17:26:21.744: E/AndroidRuntime(12764): ... 11 more
我也参考了下面的链接并使用它,但它仍然是一样的