-1

I have two buttons and two textViews , when i click "add2_A" ,then "score1" is updated and increment by 2 .

the same is "add2_B" will increment "score2"

i want to use only one method for both of them ..the problem is that the app crash !

this is my 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 : this is XML code for 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 : i solved it and it works now ..this is the 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 : this is what appears in the logcat and there's no other messages in the message bar :

  Process: com.example.mayouza.myapplication2, PID: 2553
                                                                              java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mayouza.myapplication2/com.example.mayouza.myapplication2.app2}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                                  at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
                                                                                  at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:151)
                                                                                  at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
                                                                                  at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
                                                                                  at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
                                                                                  at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:33)
                                                                                  at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
                                                                                  at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
                                                                                  at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
                                                                                  at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
                                                                                  at com.example.mayouza.myapplication2.app2.<init>(app2.java:11)
                                                                                  at java.lang.Class.newInstance(Native Method)
                                                                                  at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                                  at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:154) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
mai amer
  • 1
  • 2

1 Answers1

0

You will not be able to successfully call findViewById() before you call setContentView() because view hiearchy has not built yet.

You need to move call to findViewById() after call to setContentView().

setContentView(R.layout.activity_app2);
txt1= (TextView) findViewById(R.id.score1);
txt2 = (TextView) findViewById(R.id.score2);
Zamrony P. Juhara
  • 5,222
  • 2
  • 24
  • 40