按钮在第二次单击时崩溃,删除View()异常

时间:2013-02-17 20:09:23

标签: java android logcat

当我第一次单击计算按钮时,它工作正常,但在结果对话框被解除后第二次单击时,应用程序崩溃。logcat显示错误指定的子项已经有父项。你必须致电removeView on the child's parent first.我现在该怎么办?以及如何添加removeView?

public class MainActivity extends Activity {
    float Remaining,Departure,TotUplift,SG,DiscResult;
    int CalUpliftResult;

    TextView RemainingTV,DepartureTV,UpliftTV,SGtv,CalcUpliftTV,DiscrepancyTV,resultOne,resultTwo;
    EditText RemainingET,DepartureET,TotUpliftET,SGet,CalcUpliftET,DiscrepancyET;
    Button calculateButton,okButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        setupView();

         final AlertDialog.Builder alert = new AlertDialog.Builder(this); 
            LayoutInflater factory = LayoutInflater.from(this);        
            final View textEntryView = factory.inflate(R.layout.dialog,null);
        resultOne=(TextView)textEntryView.findViewById(R.id.resultOne); //resultone is a textview in xml dialog
        resultTwo=(TextView)textEntryView.findViewById(R.id.resultTwo);
        alert.setTitle("RESULT");
        alert.setIcon(R.drawable.ic_launcher);
        alert.setView(textEntryView);


        alert.setNeutralButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();

            }
        });








        calculateButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {


                if(validationET())
                {


                getETvalue();
                evaluation();
                CalcUpliftTV.setText(String.valueOf(CalUpliftResult));
                DiscrepancyTV.setText(String.valueOf(DiscResult));
                resultOne.setText("Calc. Uplift (KG)= "+String.valueOf(CalUpliftResult));
                resultTwo.setText("Discrepancy(%)= "+String.valueOf(DiscResult));
                    alert.show();




                }
                else
                    Toast.makeText(getApplicationContext(), "please give all inputs", Toast.LENGTH_SHORT).show();

            }


        });




    }

1 个答案:

答案 0 :(得分:0)

我修复了它,必须在计算按钮的onclick中创建alertdialog,这样每次单击它时,对话框都会重新创建,从而防止来自先前对话框值的干扰。这是更正后的代码段:

calculateButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {


        if(validationET())
        {

             final AlertDialog.Builder alert = new AlertDialog.Builder(context); 
                LayoutInflater factory = LayoutInflater.from(context);        
                final View textEntryView = factory.inflate(R.layout.dialog,null);
            resultOne=(TextView)textEntryView.findViewById(R.id.resultOne); //resultone is a textview in xml dialog
            resultTwo=(TextView)textEntryView.findViewById(R.id.resultTwo);
            alert.setTitle("RESULT");
            alert.setIcon(R.drawable.ic_launcher);
            alert.setView(textEntryView);


            alert.setNeutralButton("OK",null);




        getETvalue();
        evaluation();
        CalcUpliftTV.setText(String.valueOf(CalUpliftResult));
        DiscrepancyTV.setText(String.valueOf(DiscResult));
        resultOne.setText("Calc. Uplift (KG)= "+String.valueOf(CalUpliftResult));
        resultTwo.setText("Discrepancy(%)= "+String.valueOf(DiscResult));
        AlertDialog alertD = alert.create();
        alertD.show();






        }
        else
            Toast.makeText(getApplicationContext(), "please give all inputs", Toast.LENGTH_SHORT).show();

    }


});