0

this is supposed to be some sort of multi dialog that ask you questions and save your answers , for some reason my code crashes at my listeners

final Context context = this;
 int currentshape, goalshape; // 0 = lean , 1 = fat , 2 = skinny fat || 0 = shredded , 1 = powerlifter, 2 = bodybuiler.
double weight;
double height;
boolean dialogflag=false; // true if dialog finsihed then turned to false
Button[] current, goals; // buttons
EditText mainheight,mainweight; // question3 weight + height

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_load);
    mainheight = (EditText) findViewById(R.id.height);
    mainweight = (EditText) findViewById(R.id.weight);
    current = new Button[3];
    goals = new Button[3];

    // dialog pop
    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.question1);
    dialog.show();
    //
    current[0] = (Button) findViewById(R.id.btnLean);
    current[1] = (Button) findViewById(R.id.btnFat);
    current[2] = (Button) findViewById(R.id.btnSkinnyFat);
    for (int i = 0; i < 3; i++)
        current[i].setOnClickListener(this);
    // dialog main code
    if(dialogflag==true){ // second dialog (goal dialog)
        dialog.dismiss();
        dialog.setContentView(R.layout.question2);
        dialog.show();
        // setup buttons
        goals[0] = (Button) findViewById(R.id.btnShrededd);
        goals[1] = (Button) findViewById(R.id.btnPowerLifting);
        goals[2] = (Button) findViewById(R.id.btnBodyBuilding);
        for (int i = 0; i < 3; i++)
            goals[i].setOnClickListener(this);
        // wait for next button
        dialogflag=false;
        if(dialogflag==true){ // third dialog (height + weight dialog)
            dialog.dismiss();
            dialogflag=false;
            Intent intent = new Intent(this,MainActivity.class); // transfering to main activity.
            startActivity(intent);
            finish();
        }
    }
}

@Override
public void onClick(View v) {
    // dialog buttons.
    switch (v.getId()) {
        case R.id.btnLean:
            currentshape = 0;
            dialogflag = true;
            break;
        case R.id.btnFat:
            currentshape = 1;
            dialogflag = true;
            break;
        case R.id.btnSkinnyFat:
            currentshape = 2;
            dialogflag = true;
            break;
        case R.id.btnShrededd:
            goalshape=0;
            dialogflag=true;
            break;
        case R.id.btnPowerLifting:
            goalshape=1;
            dialogflag=true;
            break;
        case R.id.btnBodyBuilding:
            goalshape=2;
            dialogflag=true;
            break;
        case R.id.btnOK:
            weight=Double.valueOf(mainweight.getText().toString());
            height=Double.valueOf(mainweight.getText().toString());
            dialogflag=true;
            break;
    }

this is the error log

              Process: com.example.gold.mnfitness, PID: 4322
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gold.mnfitness/com.example.gold.mnfitness.First_login}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2902)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3037)
                  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
                  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
                  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
                  at android.os.Handler.dispatchMessage(Handler.java:106)
                  at android.os.Looper.loop(Looper.java:193)
                  at android.app.ActivityThread.main(ActivityThread.java:6642)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                  at com.example.gold.mnfitness.First_login.onCreate(First_login.java:44)
                  at android.app.Activity.performCreate(Activity.java:7131)
                  at android.app.Activity.performCreate(Activity.java:7122)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2882)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3037) 
                  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
                  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
                  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797) 
                  at android.os.Handler.dispatchMessage(Handler.java:106) 
                  at android.os.Looper.loop(Looper.java:193) 
                  at android.app.ActivityThread.main(ActivityThread.java:6642) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

the code supposed to show multi dialogs each dialog have diffrent xml. but for some reason it cannot put in button arr the id of button. if some one could tell me how i could should code it , this is my first post here sorry if i didnt post it right.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

Your log says that your buttons:

current[0]
current[1]
current[2]
goals[0] 
goals[1]
goals[2]

is null, please check your xml. R.layout.splash_load is your right layout?

Liar
  • 1,235
  • 1
  • 9
  • 19