0

当我第一次单击计算按钮时,它工作正常,但在结果对话框被关闭后第二次单击时,应用程序崩溃。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();

            }


        });




    }
4

1 回答 1

0

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

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();

    }


});
于 2013-02-18T16:35:18.070 回答