1

こんにちは私は、組み込みのメッセージングアプリからメッセージを取得してlistview. 会話を取得するために以下のコードを使用しています。

Uri uriSMSURI = Uri.parse("content://mms-sms/conversations/"); 

これは、Samsung Grand を除くすべての電話で正常に機能しています。私は例外を取得しています

 08-23 11:29:53.778: E/AndroidRuntime(22826): FATAL EXCEPTION: main 
 08-23 11:29:53.778: E/AndroidRuntime(22826): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.MyApp/com.example.MyApp.MainActivity}:java.lang.NullPointerException 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at android.app.ActivityThread.access$700(ActivityThread.java:140) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at android.os.Handler.dispatchMessage(Handler.java:99) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at android.os.Looper.loop(Looper.java:137) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at android.app.ActivityThread.main(ActivityThread.java:4935) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at java.lang.reflect.Method.invokeNative(Native Method) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at java.lang.reflect.Method.invoke(Method.java:511) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at dalvik.system.NativeStart.main(Native Method) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):Caused by: java.lang.NullPointerException 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at android.os.Parcel.readException(Parcel.java:1431) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at android.content.ContentProviderProxy.query(ContentProviderNative.java:366) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at android.content.ContentResolver.query(ContentResolver.java:372) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at android.content.ContentResolver.query(ContentResolver.java:315) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at com.example.MyApp.MainActivity.getSMS(MainActivity.java:188) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at com.example.MyApp.MainActivity.onCreate(MainActivity.java:64) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at android.app.Activity.performCreate(Activity.java:5206) 08-23 
 11:29:53.778: E/AndroidRuntime(22826):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074) 
 08-23 11:29:53.778: E/AndroidRuntime(22826):    ... 11 more 

これは私の MainActivity.java です

public class MainActivity extends Activity implements OnClickListener
{
  private ImageView Msg;
  private ImageView Home;
  ListView listviewsms;
  private ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Msg = (ImageView) findViewById(R.id.createmessage);
    Msg.setOnClickListener(this);

    Home = (ImageView) findViewById(R.id.home);
    Home.setOnClickListener(this);

    listviewsms = (ListView) findViewById(R.id.lvSMS);

    final List<String> msgList = getSMS();
    adapter = new ArrayAdapter<String>(this, R.layout.conversation_list,R.id.name,msgList); 

    // updating listview
    listviewsms.setAdapter(adapter);
   }

   public ArrayList<String> getSMS()
   {
    ArrayList<String> sms = new ArrayList<String>();
    Uri uriSMSURI = Uri.parse("content://sms/inbox");

    Cursor cursor = getContentResolver().query(uriSMSURI, String[] {"*"}, null, null, "date desc");

    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);

     }

    for (int i = 0; i < sms.size(); i++) 
    {
        Log.e(" "," "+sms.get(i));
    }

    return sms;
    }
     public void onClick(View v) 
    {
        //button functionality
    }
 }

これを解決するにはどうすればよいですか。助けてください。ありがとう!

4

2 に答える 2

0

このコードを試して、

public List<String> getSMS(){
     List<String> sms = new ArrayList<String>();
        Uri uriSMSURI = Uri.parse("content://sms/inbox");
        Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);

        while (cur.moveToNext()) {
               String address = cur.getString(cur.getColumnIndex("address"));
               String body = cur.getString(cur.getColumnIndexOrThrow("body"));
              sms.add("Number: " + address + " .Message: " + body);  

          }
        return sms;

}

date('now', '-1 day') と date('now') の間の日付として選択を設定しようとし、selectionArgs を削除します

アップデート:

Cursor cur = getContentResolver().query(uriSMSURI, projection, "datetime(date/1000, 'unixepoch') between date('now', '-1 day') and date('now')", null, null);

このサイトを試して、

http://www.itcuties.com/android/read-sms/
于 2013-08-23T06:21:56.487 に答える