android - Caused by: java.lang.NullPointerException on onClickListener -


this question has answer here:

i try create 1 onclicklistener multiple button , take following error

06-27 20:12:27.468: d/androidruntime(4810): shutting down vm 06-27 20:12:27.468: w/dalvikvm(4810): threadid=1: thread exiting uncaught exception (group=0x415caba8) 06-27 20:12:27.468: e/androidruntime(4810): fatal exception: main 06-27 20:12:27.468: e/androidruntime(4810): process: com.example.calculatorrr, pid: 4810 06-27 20:12:27.468: e/androidruntime(4810): java.lang.runtimeexception: unable start activity componentinfo{com.example.calculatorrr/com.example.calculatorrr.mainactivity}: java.lang.nullpointerexception 06-27 20:12:27.468: e/androidruntime(4810):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2184) 06-27 20:12:27.468: e/androidruntime(4810):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2233) 06-27 20:12:27.468: e/androidruntime(4810):     @ android.app.activitythread.access$800(activitythread.java:135) 06-27 20:12:27.468: e/androidruntime(4810):     @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 06-27 20:12:27.468: e/androidruntime(4810):     @ android.os.handler.dispatchmessage(handler.java:102) 06-27 20:12:27.468: e/androidruntime(4810):     @ android.os.looper.loop(looper.java:136) 06-27 20:12:27.468: e/androidruntime(4810):     @ android.app.activitythread.main(activitythread.java:5001) 06-27 20:12:27.468: e/androidruntime(4810):     @ java.lang.reflect.method.invokenative(native method) 06-27 20:12:27.468: e/androidruntime(4810):     @ java.lang.reflect.method.invoke(method.java:515) 06-27 20:12:27.468: e/androidruntime(4810):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785) 06-27 20:12:27.468: e/androidruntime(4810):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601) 06-27 20:12:27.468: e/androidruntime(4810):     @ dalvik.system.nativestart.main(native method) 06-27 20:12:27.468: e/androidruntime(4810): caused by: java.lang.nullpointerexception 06-27 20:12:27.468: e/androidruntime(4810):     @ com.example.calculatorrr.mainactivity.oncreate(mainactivity.java:426) 06-27 20:12:27.468: e/androidruntime(4810):     @ android.app.activity.performcreate(activity.java:5231) 06-27 20:12:27.468: e/androidruntime(4810):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 06-27 20:12:27.468: e/androidruntime(4810):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2148) 06-27 20:12:27.468: e/androidruntime(4810):     ... 11 more 

my main activitiy class this:

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();         }         framelayout mframelayout = (framelayout)findviewbyid(r.id.container);          button mbutton0 = (button) mframelayout.findviewbyid(r.id.bt0);         log.i("test1", mbutton0.tostring());         button mbutton1 = (button) mframelayout.findviewbyid(r.id.bt1);         button mbutton2 = (button) mframelayout.findviewbyid(r.id.bt2);         button mbutton3 = (button) mframelayout.findviewbyid(r.id.bt3);         button mbutton4 = (button) mframelayout.findviewbyid(r.id.bt4);         button mbutton5 = (button) mframelayout.findviewbyid(r.id.bt5);         button mbutton6 = (button) mframelayout.findviewbyid(r.id.bt6);         button mbutton7 = (button) mframelayout.findviewbyid(r.id.bt7);         button mbutton8 = (button) mframelayout.findviewbyid(r.id.bt8);         button mbutton9 = (button) mframelayout.findviewbyid(r.id.bt9);         button mbuttonmul = (button) mframelayout.findviewbyid(r.id.bt_multiplication);         button mbuttonplu = (button) mframelayout.findviewbyid(r.id.bt_plus);         button mbuttonmin = (button) mframelayout.findviewbyid(r.id.btminus);         button mbuttondiv = (button) mframelayout.findviewbyid(r.id.bt_divide);          mbutton0.setonclicklistener(onclicklistener);         mbutton1.setonclicklistener(onclicklistener);         mbutton2.setonclicklistener(onclicklistener);         mbutton3.setonclicklistener(onclicklistener);         mbutton4.setonclicklistener(onclicklistener);         mbutton5.setonclicklistener(onclicklistener);         mbutton6.setonclicklistener(onclicklistener);         mbutton7.setonclicklistener(onclicklistener);         mbutton8.setonclicklistener(onclicklistener);         mbutton9.setonclicklistener(onclicklistener);         mbuttonmul.setonclicklistener(onclicklistener);         mbuttonplu.setonclicklistener(onclicklistener);         mbuttonmin.setonclicklistener(onclicklistener);         mbuttondiv.setonclicklistener(onclicklistener);      }  private view.onclicklistener onclicklistener = new view.onclicklistener() {         @override         public void onclick(view v) {             textview txtview = (textview) findviewbyid(r.id.txt1);             string message = txtview.gettext().tostring();             switch (v.getid()) {             case r.id.bt0:                 message = message + "0";                 break;             case r.id.bt1:                 message = message + "1";                 break;             case r.id.bt2:                 message = message + "2";                 break;             case r.id.bt3:                 message = message + "3";                 break;             case r.id.bt4:                 message = message + "4";                 break;             case r.id.bt5:                 message = message + "5";                 break;             case r.id.bt6:                 message = message + "6";                 break;             case r.id.bt7:                 message = message + "7";                 break;             case r.id.bt8:                 message = message + "8";                 break;             case r.id.bt9:                 message = message + "9";                 break;             case r.id.bt_multiplication:                 message = message + "*";                 break;             case r.id.bt_plus:                 message = message + "+";                 break;             case r.id.btminus:                 message = message + "-";                 break;             case r.id.bt_divide:                 message = message + "/";                 break;             }             txtview.settext(message);         }     };  } 

my 2 resource files is:

activity_main.xml:

<framelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/container"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="com.example.calculatorrr.mainactivity"     tools:ignore="mergerootframe" /> 

and fragment_main.xml:

    <linearlayout         xmlns:android="http://schemas.android.com/apk/res/android"         android:orientation="vertical"         android:layout_width="match_parent"         android:layout_height="match_parent"         >          <textview             android:id="@+id/txt1"             android:layout_width="match_parent"             android:layout_height="66dp"             android:background="@android:color/darker_gray"             android:hint="0"             android:textsize="40dp" /> ........ other butttons 

i have search on google , here button nothing works far! maybe obvious didn't see! attention!

edit: line error:

mbutton0.setonclicklistener(onclicklistener); 

in onclicklistener textview tx1 in null,because textview txt1 part of fragment not activity_main.

so either use tx1 in activity_main layout. or use in oncreateview method of fragment.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -