android - Spinner in Action Bar returns Null Pointer Exception -


i trying implement spinner in action bar, switch between 2 different fragments in activity. implement it, have added spinner using xml in layout, , trying set adapter in base activity class. here's activity class,

protected void oncreate(bundle savedinstancestate) {     // todo auto-generated method stub     super.oncreate(savedinstancestate);     log.i(debug,"oncreate");  setcontentview(r.layout.add_new_course);  intent intent = getintent(); semesterid = intent.getintextra("semester id", 0);      log.i(tag,"add new course created");      spinner = (spinner)findviewbyid(r.id.coursespinner);     fragmentmanager fragman = getfragmentmanager();       if(savedinstancestate == null)     {         adddetailsfrag = new addcoursedetailsfragment();         addweightsfrag = new addcourseweightsfragment();          fragman.begintransaction().add(r.id.coursefragmentcontainer, adddetailsfrag);      }   }      @override public boolean oncreateoptionsmenu(menu menu) {     // todo auto-generated method stub     log.i(debug,"oncreateoptionsmenu");      getmenuinflater().inflate(r.menu.courses_menu, menu);       spinner = (spinner)findviewbyid(r.id.coursespinner);       if(spinner == null)         log.i(debug,"spinner == null");               //this prints, causing problem.          arrayadapter<charsequence> adapter = arrayadapter.createfromresource(this,r.array.coursespinner,android.r.layout.simple_spinner_item);     adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item);     spinner.setadapter(adapter);     spinner.setonitemselectedlistener(new onitemselectedlistener() {          public void onitemselected(adapterview<?> adapter, view view, int position, long id)         {              //dostuff                         }          public void onnothingselected(adapterview<?> adapter)         {                              //domorestuff         }       });      log.i(tag, "menu inflated");     return true; } 

here logcat, although it's pretty straight forward implying null-pointer:

 07-11 01:29:15.703: i/debug problem(6036): spinner == null 07-11 01:29:15.723: d/androidruntime(6036): shutting down vm 07-11 01:29:15.723: w/dalvikvm(6036): threadid=1: thread exiting uncaught exception (group=0x40a71930) 07-11 01:29:15.793: e/androidruntime(6036): fatal exception: main 07-11 01:29:15.793: e/androidruntime(6036): java.lang.nullpointerexception 07-11 01:29:15.793: e/androidruntime(6036):     @ com.photoniccannon.passbook.addnewcourse.oncreateoptionsmenu(addnewcourse.java:77) 07-11 01:29:15.793: e/androidruntime(6036):     @ android.app.activity.oncreatepanelmenu(activity.java:2490) 07-11 01:29:15.793: e/androidruntime(6036):     @ com.android.internal.policy.impl.phonewindow.preparepanel(phonewindow.java:407) 07-11 01:29:15.793: e/androidruntime(6036):     @ com.android.internal.policy.impl.phonewindow.doinvalidatepanelmenu(phonewindow.java:769) 07-11 01:29:15.793: e/androidruntime(6036):     @ com.android.internal.policy.impl.phonewindow$1.run(phonewindow.java:201) 07-11 01:29:15.793: e/androidruntime(6036):     @ android.view.choreographer$callbackrecord.run(choreographer.java:749) 07-11 01:29:15.793: e/androidruntime(6036):     @ android.view.choreographer.docallbacks(choreographer.java:562) 07-11 01:29:15.793: e/androidruntime(6036):     @ android.view.choreographer.doframe(choreographer.java:531) 07-11 01:29:15.793: e/androidruntime(6036):     @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:735) 07-11 01:29:15.793: e/androidruntime(6036):     @ android.os.handler.handlecallback(handler.java:725) 07-11 01:29:15.793: e/androidruntime(6036):     @ android.os.handler.dispatchmessage(handler.java:92) 07-11 01:29:15.793: e/androidruntime(6036):     @ android.os.looper.loop(looper.java:137) 07-11 01:29:15.793: e/androidruntime(6036):     @ android.app.activitythread.main(activitythread.java:5041) 07-11 01:29:15.793: e/androidruntime(6036):     @ java.lang.reflect.method.invokenative(native method) 07-11 01:29:15.793: e/androidruntime(6036):     @ java.lang.reflect.method.invoke(method.java:511) 07-11 01:29:15.793: e/androidruntime(6036):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 07-11 01:29:15.793: e/androidruntime(6036):     @ com.android.internal.os.zygoteinit.main        (zygoteinit.java:560) 07-11 01:29:15.793: e/androidruntime(6036):     @ dalvik.system.nativestart.main(native method) 07-11 01:34:15.983: i/process(6036): sending signal. pid: 6036 sig: 9 07-11 01:34:17.253: e/trace(6137): error opening trace file: no such file or directory (2) 

edit: courses_menu.xml

<?xml version="1.0" encoding="utf-8"?> 

<spinner     android:id="@+id/coursespinner"     android:layout_width="fill_parent"     android:layout_height="wrap_content" /> 

i don't know why it's null. have instantiate spinner spinner class?

thanks.

if coursespinner resource inside of courses_menu xml try this:

menuitem item = menu.finditem(r.id.coursespinner); spinner = (spinner)item.getactionview(); 

this because have spinner resource menu.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -