android - why would it throw an java.lang.NullPointerException -
this main activity;
package com.example.sendsms; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } public void findcontact(view view ){ intent intent = new intent(this,readcontect.class); startactivity(intent); } }
package com.example.sendsms; import java.util.arraylist; import java.util.list; import com.example.sendsms.domain.person; import android.content.contentresolver; import android.content.context; import android.database.cursor; import android.net.uri; public class getcontect{ public static list<person> getcontectinfo(context context){ contentresolver cr = context.getcontentresolver(); list<person> persons = new arraylist<person>(); uri uri = uri.parse("content://com.android.contacts/raw_contacts"); uri uridata = uri.parse("content://com.android.contacts/data"); cursor cursor = cr.query(uri, new string[] {"contact_id"} , null, null, null); while(cursor.movetonext()){ string id = cursor.getstring(0); if(id != null){ cursor datacursor = cr.query(uridata, new string[]{"data1","mimetype"}, "raw_contact_id=?", new string [] {id}, null); person per = new person() ; while(datacursor.movetonext()){ string data = datacursor.getstring(0); string mimetype =datacursor.getstring(1); if("vnd.android.cursor.item/name".equals(mimetype)){ per.setname(data); }else if ("vnd.android.cursor.item/phone_v2".equals(mimetype)){ per.setnumber(data); } } datacursor.close(); persons.add(per); } } cursor.close(); return persons; } }
package com.example.sendsms; import java.util.list; import com.example.sendsms.domain.person; import com.example.sendsms.getcontect; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.listview; import android.widget.textview; public class readcontect extends activity { private listview lv; private list<person> persons; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_read_contect); lv = (listview) findviewbyid(r.id.lv); lv.setadapter(new contectadap()); persons = getcontect.getcontectinfo(this); } private class contectadap extends baseadapter{ @override public int getcount() { // todo auto-generated method stub return persons.size(); } @override public object getitem(int arg0) { // todo auto-generated method stub return null; } @override public long getitemid(int arg0) { // todo auto-generated method stub return 0; } @override public view getview(int arg0, view arg1, viewgroup arg2) { // todo auto-generated method stub person per =persons.get(arg0); view view = view.inflate(getapplicationcontext(), r.layout.contect_view, null); textview et_name = (textview) findviewbyid(r.id.name); textview et_number = (textview) findviewbyid(r.id.number); et_name.settext(per.getname()); et_name.settext(per.getnumber()); return view; } } }
03-28 11:30:52.467: w/dalvikvm(4455): threadid=1: thread exiting uncaught exception (group=0xb1d8cb20) 03-28 11:30:54.407: e/androidruntime(4455): fatal exception: main 03-28 11:30:54.407: e/androidruntime(4455): process: com.example.sendsms, pid: 4455 03-28 11:30:54.407: e/androidruntime(4455): java.lang.runtimeexception: unable start activity componentinfo{com.example.sendsms/com.example.sendsms.readcontect}: java.lang.nullpointerexception 03-28 11:30:54.407: e/androidruntime(4455): at android.app.activitythread.performlaunchactivity(activitythread.java:2195) 03-28 11:30:54.407: e/androidruntime(4455): at android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 03-28 11:30:54.407: e/androidruntime(4455): at android.app.activitythread.access$800(activitythread.java:135) 03-28 11:30:54.407: e/androidruntime(4455): at android.app.activitythread$h.handlemessage(activitythread.java:1196) 03-28 11:30:54.407: e/androidruntime(4455): at android.os.handler.dispatchmessage(handler.java:102) 03-28 11:30:54.407: e/androidruntime(4455): at android.os.looper.loop(looper.java:136) 03-28 11:30:54.407: e/androidruntime(4455): at android.app.activitythread.main(activitythread.java:5017) 03-28 11:30:54.407: e/androidruntime(4455): at java.lang.reflect.method.invokenative(native method) 03-28 11:30:54.407: e/androidruntime(4455): at java.lang.reflect.method.invoke(method.java:515) 03-28 11:30:54.407: e/androidruntime(4455): at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 03-28 11:30:54.407: e/androidruntime(4455): at com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 03-28 11:30:54.407: e/androidruntime(4455): at dalvik.system.nativestart.main(native method) 03-28 11:30:54.407: e/androidruntime(4455): caused by: java.lang.nullpointerexception 03-28 11:30:54.407: e/androidruntime(4455): at com.example.sendsms.readcontect$contectadap.getcount(readcontect.java:39) 03-28 11:30:54.407: e/androidruntime(4455): at android.widget.listview.setadapter(listview.java:480) 03-28 11:30:54.407: e/androidruntime(4455): at com.example.sendsms.readcontect.oncreate(readcontect.java:26) 03-28 11:30:54.407: e/androidruntime(4455): at android.app.activity.performcreate(activity.java:5231) 03-28 11:30:54.407: e/androidruntime(4455): at android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 03-28 11:30:54.407: e/androidruntime(4455): at android.app.activitythread.performlaunchactivity(activitythread.java:2159)
maybe expression not clear enough ,thanks patience;
Comments
Post a Comment