这应该是一种多对话框,可以询问您的问题并保存您的答案,出于某种原因,我的代码在监听器上崩溃了
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;
}
这是错误日志
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)
应该显示多个对话框的代码,每个对话框具有不同的xml。 但由于某种原因,它无法在按钮arr中放置按钮的ID。 如果有人可以告诉我我应该如何编码,这是我在这里的第一篇文章,对不起,如果我没有正确地张贴它。
答案 0 :(得分:0)
您的日志显示您的按钮:
current[0]
current[1]
current[2]
goals[0]
goals[1]
goals[2]
为空,请检查您的xml。 R.layout.splash_load是您正确的布局吗?