使用LayoutInflater和AlertDialog

时间:2013-06-27 08:31:13

标签: android nullpointerexception android-alertdialog layout-inflater

我试图让一个AlertDialog从自定义视图中膨胀。我想我知道问题是什么,但我不知道如何解决它。我在LongClick上有一个(View V),而Button1正在使用“alertdialog”来查找数据。我在网上得到一个NullPointer异常 TextView stax1 =(TextView)findViewById(R.id.xxx); 唯一有意义的是我让Dialog在查看“alerthelp”时查看它来自的活动。我该如何解决这个问题?

        lay1.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v)        
        {                     

            LayoutInflater myLayout = LayoutInflater.from(context);
            View dialogView = myLayout.inflate(R.layout.alerthelp, null);
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context);
            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialogBuilder.setView(dialogView);
            alertDialog.show();


            Button button1 = (Button)dialogView.findViewById(R.id.button1);
            button1.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                TextView stax1=(TextView)findViewById(R.id.xxx);
                String sax1 = stax1.getText().toString();
                double sx1 = Double.parseDouble(sax1);

                TextView textviewlay1 =(TextView)findViewById(R.id.m1ab);
                String stringl1 = textviewlay1.getText().toString();
                 Double doubl1 = Double.parseDouble(stringl1);  

                TextView textviewp1 = (TextView)findViewById(R.id.inputPrice);
                String stringp1 = textviewp1.getText().toString();
                Double intp1 = Double.parseDouble(stringp1);

                double resultl1 = intp1 - (doubl1*sx1);
                int precision21t = 100; //keep 4 digits
                resultl1= Math.floor(resultl1 * precision21t +.5)/precision21t;
                textViewtotalproduct.setText(String.valueOf(resultl1));     

            }
        }); 


            Button button2 = (Button) dialogView.findViewById(R.id.button2); 
            button2.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                TextView textviewlay1 =(TextView)findViewById(R.id.m1ab);
                String stringl1 = textviewlay1.getText().toString();
                Double doubl1 = Double.parseDouble(stringl1);    

                TextView textviewp1 = (TextView)findViewById(R.id.inputPrice);
                String stringp1 = textviewp1.getText().toString();
                Double intp1 = Double.parseDouble(stringp1);

                double resultl1 = intp1 - doubl1;
                int precision21t = 100; //keep 4 digits
                resultl1= Math.floor(resultl1 * precision21t +.5)/precision21t;
                textViewtotalproduct.setText(String.valueOf(resultl1));  
                }
                }); 

            Button button3 = (Button)dialogView.findViewById(R.id.button3); 
            button3.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                TextView ss1=(TextView)findViewById(R.id.s2);
                ss1.setText("st");

            }
        });


        Button button4 = (Button)dialogView.findViewById(R.id.button4);
        button4.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            TextView ss1=(TextView)findViewById(R.id.s2);
                ss1.setText("");


            }
        }); 



        Button button5 = (Button)dialogView.findViewById(R.id.button5); 
        button5.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                Intent mainIntent = new Intent(xxxActivity.this, SettingsActivity.class);
                MenuView1Activity.this.startActivity(mainIntent);
                MenuView1Activity.this.finish();
            }
        }); 

        Button button6 = (Button)dialogView.findViewById(R.id.button6); 
        button6.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                Intent mainIntent = new Intent(xxxActivity.this, SettingsActivity.class);
                xxxActivity.this.startActivity(mainIntent);
                xxxActivity.this.finish();
            }
        });
        return false;   




        }});

2 个答案:

答案 0 :(得分:5)

您似乎正在尝试从静态TextView加载文本。可能应该是EditText?

TextView stax1=(TextView)dialogView.findViewById(R.id.xxx);
String sax1 = stax1.getText().toString();

编辑:

试试这段代码:

lay1.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v)        
        {                     

            LayoutInflater myLayout = LayoutInflater.from(context);
            final View dialogView = myLayout.inflate(R.layout.alerthelp, null);
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context);
            alertDialogBuilder.setView(dialogView);

            AlertDialog alertDialog = alertDialogBuilder.create();
            Button button1 = (Button) alertDialog.findViewById(R.id.button1);
            button1.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                TextView stax1=(TextView)dialogView.findViewById(R.id.xxx);
                String sax1 = stax1.getText().toString();
                double sx1 = Double.parseDouble(sax1);
                TextView textviewlay1 =(TextView)dialogView.findViewById(R.id.m1ab);
                String stringl1 = textviewlay1.getText().toString();
                 Double doubl1 = Double.parseDouble(stringl1);      
                TextView textviewp1 = (TextView)dialogView.findViewById(R.id.inputPrice);
                String stringp1 = textviewp1.getText().toString();
                Double intp1 = Double.parseDouble(stringp1);
                double resultl1 = intp1 - (doubl1*sx1);
                int precision21t = 100; //keep 4 digits
                resultl1= Math.floor(resultl1 * precision21t +.5)/precision21t;
                textViewtotalproduct.setText(String.valueOf(resultl1));     

            }
        }); 

 return false;  




        }});

答案 1 :(得分:1)

更改

TextView stax1=(TextView)findViewById(R.id.xxx);

TextView stax1=(TextView)dialogView.findViewById(R.id.xxx);

希望这有帮助。