android - Getting error for creating and populating ListView -


i trying list of installed applications on device , display names in list.
when run code shows unfortunately,lister has been stopped.
code in oncreate method is:-

packagemanager pm=this.getpackagemanager();         list<applicationinfo> list=pm.getinstalledapplications(0);         listview lv=(listview)findviewbyid(r.id.listview1);         arraylist<string> al=new arraylist<string>();         for(applicationinfo app:list)         {             string appn=pm.getapplicationlabel(app).tostring();             al.add(appn);          }         arrayadapter<string> arr=new arrayadapter<string>(this, android.r.layout.simple_list_item_1,android.r.id.text1,al);         lv.setadapter(arr);         setcontentview(r.layout.activity_sec); 

and activity_sec.xml code is:-

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:gravity="top"     android:paddingbottom="@dimen/activity_vertical_margin"     android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     tools:context="com.example.gestureview.sec" >      <listview         android:id="@+id/listview1"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_alignparentleft="true"         android:layout_alignparenttop="true"         android:layout_marginleft="40dp" >     </listview>  </relativelayout> 

my log cat is:-

06-19 04:33:53.234: e/androidruntime(1205): fatal exception: main 06-19 04:33:53.234: e/androidruntime(1205): java.lang.runtimeexception: unable start activity componentinfo{com.example.gestureview/com.example.gestureview.sec}: java.lang.nullpointerexception 06-19 04:33:53.234: e/androidruntime(1205):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 06-19 04:33:53.234: e/androidruntime(1205):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 06-19 04:33:53.234: e/androidruntime(1205):     @ android.app.activitythread.access$600(activitythread.java:141) 06-19 04:33:53.234: e/androidruntime(1205):     @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 06-19 04:33:53.234: e/androidruntime(1205):     @ android.os.handler.dispatchmessage(handler.java:99) 06-19 04:33:53.234: e/androidruntime(1205):     @ android.os.looper.loop(looper.java:137) 06-19 04:33:53.234: e/androidruntime(1205):     @ android.app.activitythread.main(activitythread.java:5041) 06-19 04:33:53.234: e/androidruntime(1205):     @ java.lang.reflect.method.invokenative(native method) 06-19 04:33:53.234: e/androidruntime(1205):     @ java.lang.reflect.method.invoke(method.java:511) 06-19 04:33:53.234: e/androidruntime(1205):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 06-19 04:33:53.234: e/androidruntime(1205):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 06-19 04:33:53.234: e/androidruntime(1205):     @ dalvik.system.nativestart.main(native method) 06-19 04:33:53.234: e/androidruntime(1205): caused by: java.lang.nullpointerexception 06-19 04:33:53.234: e/androidruntime(1205):     @ com.example.gestureview.sec.oncreate(sec.java:33) 06-19 04:33:53.234: e/androidruntime(1205):     @ android.app.activity.performcreate(activity.java:5104) 06-19 04:33:53.234: e/androidruntime(1205):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 06-19 04:33:53.234: e/androidruntime(1205):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 06-19 04:33:53.234: e/androidruntime(1205):     ... 11 more 

it seems there must problem creating listview.

you should first set content call listview

packagemanager pm=this.getpackagemanager();     list<applicationinfo> list=pm.getinstalledapplications(0);     setcontentview(r.layout.activity_sec);    ///call line here     listview lv=(listview)findviewbyid(r.id.listview1);     arraylist<string> al=new arraylist<string>();     for(applicationinfo app:list)     {         string appn=pm.getapplicationlabel(app).tostring();         al.add(appn);      }     arrayadapter<string> arr=new arrayadapter<string>(this, android.r.layout.simple_list_item_1,android.r.id.text1,al);     lv.setadapter(arr); 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -