java - Android - Setting ImageAdapter NullPointerException -


i have problem android. want set imageadapter gallery using code:

integer[] images = {r.drawable.image01};  static int counter = 0;  sectionspageradapter msectionspageradapter; viewpager mviewpager;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      // set action bar.     final actionbar actionbar = getactionbar();     actionbar.setnavigationmode(actionbar.navigation_mode_tabs);      // create adapter return fragment each of 3     // primary sections of app.     msectionspageradapter = new sectionspageradapter(             getsupportfragmentmanager());      // set viewpager sections adapter.     mviewpager = (viewpager) findviewbyid(r.id.pager);     mviewpager.setadapter(msectionspageradapter);      // when swiping between different sections, select corresponding     // tab. can use actionbar.tab#select() if have     // reference tab.     mviewpager             .setonpagechangelistener(new viewpager.simpleonpagechangelistener() {                 @override                 public void onpageselected(int position) {                     actionbar.setselectednavigationitem(position);                 }             });      // each of sections in app, add tab action bar.     (int = 0; < msectionspageradapter.getcount(); i++) {         // create tab text corresponding page title defined         // adapter. specify activity object, implements         // tablistener interface, callback (listener) when         // tab selected.         actionbar.addtab(actionbar.newtab()                 .settext(msectionspageradapter.getpagetitle(i))                 .settablistener(this));     }      gallery g = (gallery) findviewbyid(r.id.gallery1);     g.setadapter(new imageadapter(this, images)); } 

i want add image "image01" gallery using imageadapter. code causes nullpointerexception. code of imageadapter is:

public class imageadapter extends baseadapter {       context m;     public imageadapter(context c){         m = c;     }      @override     public int getcount() {         return counter;     }      @override     public object getitem(int position) {         return position;     }      @override     public long getitemid(int position) {         return position;     }      @override     public view getview(int position, view convertview, viewgroup parent) {         imageview = new imageview(m);         i.setimageresource(images[position]);         i.setlayoutparams(new gallery.layoutparams(150, 150));         i.setscaletype(imageview.scaletype.fit_xy);         return i;      } } 

edit: xml is:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical" >      <gallery         android:id="@+id/gallery1"         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="17.92"         android:spacing="@dimen/activity_horizontal_margin" />  </linearlayout> 

the stacktrace is:

04-05 22:11:38.144: d/androidruntime(32124): shutting down vm 04-05 22:11:38.144: w/dalvikvm(32124): threadid=1: thread exiting uncaught exception (group=0x40ebf930) 04-05 22:11:38.144: e/androidruntime(32124): fatal exception: main 04-05 22:11:38.144: e/androidruntime(32124): java.lang.runtimeexception: unable start activity  componentinfo{com.bw2801.uwelugemediathek/com.bw2801.uwelugemediathek.mainactivity}:   java.lang.nullpointerexception 04-05 22:11:38.144: e/androidruntime(32124):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 04-05 22:11:38.144: e/androidruntime(32124):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 04-05 22:11:38.144: e/androidruntime(32124):    @ android.app.activitythread.access$600(activitythread.java:141) 04-05 22:11:38.144: e/androidruntime(32124):    @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 04-05 22:11:38.144: e/androidruntime(32124):    @ android.os.handler.dispatchmessage(handler.java:99) 04-05 22:11:38.144: e/androidruntime(32124):    @ android.os.looper.loop(looper.java:137) 04-05 22:11:38.144: e/androidruntime(32124):    @ android.app.activitythread.main(activitythread.java:5041) 04-05 22:11:38.144: e/androidruntime(32124):    @ java.lang.reflect.method.invokenative(native method) 04-05 22:11:38.144: e/androidruntime(32124):    @ java.lang.reflect.method.invoke(method.java:511) 04-05 22:11:38.144: e/androidruntime(32124):    @    com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 04-05 22:11:38.144: e/androidruntime(32124):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 04-05 22:11:38.144: e/androidruntime(32124):    @ dalvik.system.nativestart.main(native   method) 04-05 22:11:38.144: e/androidruntime(32124): caused by: java.lang.nullpointerexception 04-05 22:11:38.144: e/androidruntime(32124):    @ com.bw2801.uwelugemediathek.mainactivity.oncreate(mainactivity.java:79) 04-05 22:11:38.144: e/androidruntime(32124):    @ android.app.activity.performcreate(activity.java:5104) 04-05 22:11:38.144: e/androidruntime(32124):    @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 04-05 22:11:38.144: e/androidruntime(32124):    @     android.app.activitythread.performlaunchactivity(activitythread.java:2144) 04-05 22:11:38.144: e/androidruntime(32124):    ... 11 more 

the images object have doesnt exist in adapter, add new param constructor of adapter accept images

public class imageadapter extends baseadapter {       context m;     integer[] images      public imageadapter(context c, integer[] images ){         m = c;         this.images = images     } ... 

then call in activity :

 g.setadapter(new imageadapter(this, images)); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -