android - build java method to change two textViews -




i have 2 buttons , 2 textviews , when click "add2_a" ,then "score1" updated , increment 2 .

the same "add2_b" increment "score2"

i want use 1 method both of them ..the problem app crash !

this java code :

import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.textview;  public class app2 extends appcompatactivity implements view.onclicklistener  { public int number =0; public int id; textview txt1= (textview) findviewbyid(r.id.score1); textview txt2 = (textview) findviewbyid(r.id.score2); @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_app2);  } @override public void onclick(view v){    id=v.getid(); }  public void increment2(){     number =number +2;     if(id ==r.id.add2_a)         txt1.settext(number);     if(id== r.id.add2_b)         txt2.settext(string.valueof(number)); }} 

edit : xml code calling "increment2" method :

<button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/points2" android:layout_margin="8dp" android:id="@+id/add2_a" android:onclick="increment2"/> 

edit : solved , works ..this code after editing :

public class app2 extends appcompatactivity  { public int number =0; public int id; public textview txt1; public textview txt2; @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_app2);     txt1= (textview) findviewbyid(r.id.score1);     txt2 = (textview) findviewbyid(r.id.score2); }  public void increment2(view v) {     number = number + 2;     id = v.getid();     if (id == r.id.add2_a) {         txt1 = (textview) findviewbyid(r.id.score1);         txt1.settext(string.valueof(number));     }     if (id == r.id.add2_b) {         txt2 = (textview) findviewbyid(r.id.score2);         txt2.settext(string.valueof(number));     } } 

edit : appears in logcat , there's no other messages in message bar :

  process: com.example.mayouza.myapplication2, pid: 2553                                                                               java.lang.runtimeexception: unable instantiate activity componentinfo{com.example.mayouza.myapplication2/com.example.mayouza.myapplication2.app2}: java.lang.nullpointerexception: attempt invoke virtual method 'android.view.window$callback android.view.window.getcallback()' on null object reference                                                                                   @ android.app.activitythread.performlaunchactivity(activitythread.java:2567)                                                                                   @ android.app.activitythread.handlelaunchactivity(activitythread.java:2726)                                                                                   @ android.app.activitythread.-wrap12(activitythread.java)                                                                                   @ android.app.activitythread$h.handlemessage(activitythread.java:1477)                                                                                   @ android.os.handler.dispatchmessage(handler.java:102)                                                                                   @ android.os.looper.loop(looper.java:154)                                                                                   @ android.app.activitythread.main(activitythread.java:6119)                                                                                   @ java.lang.reflect.method.invoke(native method)                                                                                   @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:886)                                                                                   @ com.android.internal.os.zygoteinit.main(zygoteinit.java:776)                                                                                caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.view.window$callback android.view.window.getcallback()' on null object reference                                                                                   @ android.support.v7.app.appcompatdelegateimplbase.<init>(appcompatdelegateimplbase.java:120)                                                                                   @ android.support.v7.app.appcompatdelegateimplv9.<init>(appcompatdelegateimplv9.java:151)                                                                                   @ android.support.v7.app.appcompatdelegateimplv11.<init>(appcompatdelegateimplv11.java:31)                                                                                   @ android.support.v7.app.appcompatdelegateimplv14.<init>(appcompatdelegateimplv14.java:55)                                                                                   @ android.support.v7.app.appcompatdelegateimplv23.<init>(appcompatdelegateimplv23.java:33)                                                                                   @ android.support.v7.app.appcompatdelegateimpln.<init>(appcompatdelegateimpln.java:33)                                                                                   @ android.support.v7.app.appcompatdelegate.create(appcompatdelegate.java:201)                                                                                   @ android.support.v7.app.appcompatdelegate.create(appcompatdelegate.java:185)                                                                                   @ android.support.v7.app.appcompatactivity.getdelegate(appcompatactivity.java:519)                                                                                   @ android.support.v7.app.appcompatactivity.findviewbyid(appcompatactivity.java:190)                                                                                   @ com.example.mayouza.myapplication2.app2.<init>(app2.java:11)                                                                                   @ java.lang.class.newinstance(native method)                                                                                   @ android.app.instrumentation.newactivity(instrumentation.java:1078)                                                                                   @ android.app.activitythread.performlaunchactivity(activitythread.java:2557)                                                                                   @ android.app.activitythread.handlelaunchactivity(activitythread.java:2726)                                                                                    @ android.app.activitythread.-wrap12(activitythread.java)                                                                                    @ android.app.activitythread$h.handlemessage(activitythread.java:1477)                                                                                    @ android.os.handler.dispatchmessage(handler.java:102)                                                                                    @ android.os.looper.loop(looper.java:154)                                                                                    @ android.app.activitythread.main(activitythread.java:6119)                                                                                    @ java.lang.reflect.method.invoke(native method)                                                                                    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:886)                                                                                    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:776)  

you not able call findviewbyid() before call setcontentview() because view hiearchy has not built yet.

you need move call findviewbyid() after call setcontentview().

setcontentview(r.layout.activity_app2); txt1= (textview) findviewbyid(r.id.score1); txt2 = (textview) findviewbyid(r.id.score2); 




wiki

Comments

Popular posts from this blog

python - Read npy file directly from S3 StreamingBody -

python - Minimize function with Scipy minimize -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -