1

我正在尝试创建多问题对话框。似乎一切正常,但我无法获得 numberpicker 的结果 + 我最后有泄漏窗口

这是我的代码:

final Context context = this;
int currentshape, goalshape=9,qnumber=1,gender,age; // 0 = lean , 1 = fat , 2 = skinny fat || 0 = shredded , 1 = powerlifter, 2 = bodybuiler.
int height,weight;
Boolean flag=false,flag2=false,flag3=false;
int year;
Button[] current, goals; // buttons
public static final String PREFS_NAME = "MyApp_Settings";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.question1);
     current = new Button[3];
    goals = new Button[3];
    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);
    }

    public void questions1() {
        Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.question2);
            if(flag2 == false)
            dialog.show();
            Button[] btn = new Button[3];
            btn[0] = (Button) dialog.findViewById(R.id.btnShrededd);
            btn[1] = (Button) dialog.findViewById(R.id.btnBodyBuilding);
            btn[2] = (Button) dialog.findViewById(R.id.btnPowerLifting);
            for (int i = 0; i < 3; i++)
                btn[i].setOnClickListener(this);
            if(flag2 == true) {
                dialog.dismiss();
            }
       this.flag2=true;
}
private double parseDouble(String s){
    if(s == null || s.isEmpty())
        return 0.0;
    else
        return Double.parseDouble(s);
}
public void questions2() {
    Dialog dialog2 = new Dialog(context);
        dialog2.setContentView(R.layout.question3);
        if(flag == false && flag3 == false) {
            dialog2.show();
            Button[] btn = new Button[3];
            final NumberPicker[] tv = new NumberPicker[3];
            tv[0] = (NumberPicker) dialog2.findViewById(R.id.weight);
            tv[1] = (NumberPicker) dialog2.findViewById(R.id.height);
            tv[2] = (NumberPicker) dialog2.findViewById(R.id.age);
            btn[0] = (Button) dialog2.findViewById(R.id.btnmale);
            btn[1] = (Button) dialog2.findViewById(R.id.btnwomen);
            btn[2] = (Button) dialog2.findViewById(R.id.btnsetupfinish);
            for (int i = 0; i < 3; i++) {
                btn[i].setOnClickListener(this);
                tv[i].setMinValue(1);
                tv[i].setMaxValue(150);
            }
            if (flag == true && flag3 == false) {
                this.weight = tv[0].getValue();
                this.height = tv[1].getValue();
                this.age = tv[2].getValue();
                this.flag3 = true;
            }
        }
        if (flag3){
            dialog2.dismiss();
        }
        flag = true;
    }

@Override
protected void onPause() {
    super.onPause();
    questions2();
    Log.d("crash",this.weight + " " + this.height);
}

public void updatesharepref(){
    SharedPreferences sharedPref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btnLean:
            this.currentshape = 0;
            questions1();
            break;
        case R.id.btnFat:
            this.currentshape = 1;
            questions1();
            break;
        case R.id.btnSkinnyFat:
            this.currentshape = 2;
            questions1();
            break;
        case R.id.btnShrededd:
            this.goalshape=0;
            questions2();
            break;
        case R.id.btnPowerLifting:
            this.goalshape=1;
            questions2();
            break;
        case R.id.btnBodyBuilding:
            this.goalshape=2;
            questions2();
            break;
        case R.id.btnmale:
            this.gender=1;
            break;
        case R.id.btnwomen:
            this.gender=2;
            break;
        case R.id.btnsetupfinish:
            flag=true;
            questions2();
            Intent intent = new Intent(this,MainActivity.class);
            startActivity(intent);
            finish();
            break;
    }

我知道代码不是最漂亮的,但我是初学者。我尝试提出多对话问题,以便获取有关用户的信息。完成对话后,我无法在 NumberPicker 小部件中收到数字,因为它始终返回 0。另外,问题 2 对话框一直在泄漏。

错误日志:

E/WindowManager: android.view.WindowLeaked: Activity com.example.gold.mnfitness.First_login has leaked window DecorView@5ed0dc1[] that was originally added here
                 at android.view.ViewRootImpl.<init>(ViewRootImpl.java:485)
                 at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:346)
                 at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:92)
                 at android.app.Dialog.show(Dialog.java:330)
                 at com.example.gold.mnfitness.First_login.questions2(First_login.java:82)
                 at com.example.gold.mnfitness.First_login.onClick(First_login.java:144)
4

0 回答 0