java - Android Fragment causes NullPointerException -


first going know there lot of similar posts this, , have seen them, new android development , not yet familiar enough modify other answers fix exact problem.

i have fragment created default new project contains button , text view, , in mainactivity.java i've made onclick function button. function throws npe. here's logcat:

03-10 19:26:52.620: d/androidruntime(878): shutting down vm 03-10 19:26:52.620: w/dalvikvm(878): threadid=1: thread exiting uncaught exception (group=0xb2af2ba8) 03-10 19:26:52.630: e/androidruntime(878): fatal exception: main 03-10 19:26:52.630: e/androidruntime(878): process: com.example.main, pid: 878  03-10 19:26:52.630: e/androidruntime(878): java.lang.runtimeexception: unable start activity componentinfo{com.example.main/com.example.main.mainactivity}: java.lang.nullpointerexception 03-10 19:26:52.630: e/androidruntime(878):  @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) 03-10 19:26:52.630: e/androidruntime(878):  @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 03-10 19:26:52.630: e/androidruntime(878):  @ android.app.activitythread.access$800(activitythread.java:135) 03-10 19:26:52.630: e/androidruntime(878):  @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 03-10 19:26:52.630: e/androidruntime(878):  @ android.os.handler.dispatchmessage(handler.java:102) 03-10 19:26:52.630: e/androidruntime(878):  @ android.os.looper.loop(looper.java:136) 03-10 19:26:52.630: e/androidruntime(878):  @ android.app.activitythread.main(activitythread.java:5017) 03-10 19:26:52.630: e/androidruntime(878):  @ java.lang.reflect.method.invokenative(native method) 03-10 19:26:52.630: e/androidruntime(878):  @ java.lang.reflect.method.invoke(method.java:515) 03-10 19:26:52.630: e/androidruntime(878):  @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 03-10 19:26:52.630: e/androidruntime(878):  @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 03-10 19:26:52.630: e/androidruntime(878):  @ dalvik.system.nativestart.main(native method) 03-10 19:26:52.630: e/androidruntime(878): caused by: java.lang.nullpointerexception 03-10 19:26:52.630: e/androidruntime(878):  @ com.example.main.mainactivity.oncreate(mainactivity.java:31) 03-10 19:26:52.630: e/androidruntime(878):  @ android.app.activity.performcreate(activity.java:5231) 03-10 19:26:52.630: e/androidruntime(878):  @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 03-10 19:26:52.630: e/androidruntime(878):  @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) 03-10 19:26:52.630: e/androidruntime(878):  ... 11 more 03-10 19:31:54.292: i/process(878): sending signal. pid: 878 sig: 9 

the mainactivity.java:

package com.example.main;  import android.support.v7.app.actionbaractivity; ...  public class mainactivity extends actionbaractivity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          if (savedinstancestate == null) {             getsupportfragmentmanager().begintransaction()                     .add(r.id.container, new     placeholderfragment()).commit();         }          final textview mytext = (textview) findviewbyid(r.id.text1);         button mybutton = (button) findviewbyid(r.id.button1);          mybutton.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view v) {                 mytext.settext("yes!");             }         });     }      @override     public boolean oncreateoptionsmenu(menu menu) {          // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();         if (id == r.id.action_settings) {             return true;         }         return super.onoptionsitemselected(item);     }      /**      * placeholder fragment containing simple view.      */     public static class placeholderfragment extends fragment {          public placeholderfragment() {         }          @override         public view oncreateview(layoutinflater inflater, viewgroup container,                 bundle savedinstancestate) {             view rootview = inflater.inflate(r.layout.fragment_main, container,                     false);             return rootview;         }     }  }   

if textview , button in layout's fragment, need find id in oncreatedview method of fragment. then, should do:

public static class placeholderfragment extends fragment {      public placeholderfragment() {     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate) {         view rootview = inflater.inflate(r.layout.fragment_main, container,                 false);         final textview mytext = (textview) rootview.findviewbyid(r.id.text1);         button mybutton = (button) rootview.findviewbyid(r.id.button1);         mybutton.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 mytext.settext("yes!");             }         });         return rootview;     } } 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -