1

Hi I am developing android SMS app where I am trying to retrieve messages from built in messaging app and display it on a listview. I am using the below code for retrieving conversations.

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

This is working fine on all phones except Samsung Grand. I am getting exception as

 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 

This is my 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
    }
 }

How do I resolve this. Please Help. Thanks!

sanjana
  • 641
  • 2
  • 15
  • 36

1 Answers1

3

For samsung phones you need to use this uri Uri.parse("content://mms-sms/conversations?simple=true");

And do not pass null as the projection.

Add new String[] {"*"} as the projection.

Tarun
  • 13,727
  • 8
  • 42
  • 57
  • Uri.parse("content://mms-sms/conversations?simple=true") should work for samsung devices. In this case you wont get thread_id but _id. SO my question is did you get the same NPE when you tried this URI? – Tarun Aug 24 '13 at 06:05
  • I tried the above code with Async .Now I get a different error as http://txtup.co/HpRWV – sanjana Aug 24 '13 at 06:43
  • Right, this URI worked. Problem is with your cursor columns that you are trying to access. Do one thing, print the column names by cursor.getColumnNames() and check if you are accessing the right columns. – Tarun Aug 24 '13 at 07:05
  • `Failed to read row 0, column -1 from a CursorWindow which has 2 rows, 16 columns.` This clearly says you got 2 results from the query with 16 columns. But you accessed the wrong column index. – Tarun Aug 24 '13 at 07:06
  • Ya.My phone has two messages i.e 2 rows.but How do I solve this – sanjana Aug 24 '13 at 08:48
  • Told you what to do. Print the columnnames – Tarun Aug 24 '13 at 08:52
  • You need to tell me how you are accessing the columns.. My guess is you are trying to access thread_id. – Tarun Aug 24 '13 at 08:55
  • http://txtup.co/brujN This is what I have tried and I am getting same Async Error. – sanjana Aug 24 '13 at 10:11
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36163/discussion-between-tarun-and-sanjana) – Tarun Aug 24 '13 at 13:56
  • What is line number 207 at com.example.MyApp.MainActivity.getSMS(MainActivity.java:207)?? – Tarun Aug 24 '13 at 13:57
  • http://txtup.co/zcXSm This is what I am trying on line number 207.If u are around can we converse on chat – sanjana Aug 26 '13 at 04:02
  • can we discuss on chat – sanjana Aug 26 '13 at 05:35
  • ya I am able to retrieve it by content://mms-sms/canonical-addresses. – sanjana Aug 28 '13 at 07:05
  • Ok. great. How are you differentiating between samsung and other phones? – Tarun Aug 28 '13 at 07:07
  • This is also found using the new Telephony APIs present in KitKat. As is this a public API, I have raised this as a bug with Google https://code.google.com/p/android/issues/detail?id=71848&thanks=71848&ts=1403126924 – Alex Curran Jun 18 '14 at 21:30