hi developing android sms app trying retrieve messages built in messaging app , display on listview
. using below code retrieving conversations.
uri urismsuri = uri.parse("content://mms-sms/conversations/");
this working fine on phones except samsung grand. getting exception
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 start activity componentinfo{com.example.myapp/com.example.myapp.mainactivity}:java.lang.nullpointerexception 08-23 11:29:53.778: e/androidruntime(22826): @ android.app.activitythread.performlaunchactivity(activitythread.java:2110) 08-23 11:29:53.778: e/androidruntime(22826): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2135) 08-23 11:29:53.778: e/androidruntime(22826): @ android.app.activitythread.access$700(activitythread.java:140) 08-23 11:29:53.778: e/androidruntime(22826): @ android.app.activitythread$h.handlemessage(activitythread.java:1237) 08-23 11:29:53.778: e/androidruntime(22826): @ android.os.handler.dispatchmessage(handler.java:99) 08-23 11:29:53.778: e/androidruntime(22826): @ android.os.looper.loop(looper.java:137) 08-23 11:29:53.778: e/androidruntime(22826): @ android.app.activitythread.main(activitythread.java:4935) 08-23 11:29:53.778: e/androidruntime(22826): @ java.lang.reflect.method.invokenative(native method) 08-23 11:29:53.778: e/androidruntime(22826): @ java.lang.reflect.method.invoke(method.java:511) 08-23 11:29:53.778: e/androidruntime(22826): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1038) 08-23 11:29:53.778: e/androidruntime(22826): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:805) 08-23 11:29:53.778: e/androidruntime(22826): @ 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): @ android.os.parcel.readexception(parcel.java:1431) 08-23 11:29:53.778: e/androidruntime(22826): @ android.database.databaseutils.readexceptionfromparcel(databaseutils.java:188) 08-23 11:29:53.778: e/androidruntime(22826): @ android.database.databaseutils.readexceptionfromparcel(databaseutils.java:140) 08-23 11:29:53.778: e/androidruntime(22826): @ android.content.contentproviderproxy.query(contentprovidernative.java:366) 08-23 11:29:53.778: e/androidruntime(22826): @ android.content.contentresolver.query(contentresolver.java:372) 08-23 11:29:53.778: e/androidruntime(22826): @ android.content.contentresolver.query(contentresolver.java:315) 08-23 11:29:53.778: e/androidruntime(22826): @ com.example.myapp.mainactivity.getsms(mainactivity.java:188) 08-23 11:29:53.778: e/androidruntime(22826): @ com.example.myapp.mainactivity.oncreate(mainactivity.java:64) 08-23 11:29:53.778: e/androidruntime(22826): @ android.app.activity.performcreate(activity.java:5206) 08-23 11:29:53.778: e/androidruntime(22826): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1094) 08-23 11:29:53.778: e/androidruntime(22826): @ android.app.activitythread.performlaunchactivity(activitythread.java:2074) 08-23 11:29:53.778: e/androidruntime(22826): ... 11 more
this 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 contact name of 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); } (int = 0; < sms.size(); i++) { log.e(" "," "+sms.get(i)); } return sms; } public void onclick(view v) { //button functionality } }
how resolve this. please help. thanks!
for samsung phones need use uri uri.parse("content://mms-sms/conversations?simple=true");
and not pass null
projection.
add new string[] {"*"}
projection.