使用LayoutInflater和AlertDialog [英] Using LayoutInflater and AlertDialog

查看:153
本文介绍了使用LayoutInflater和AlertDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去得到一个AlertDialog从customview膨胀。我想我知道问题是什么,但我不知道如何解决它。我有LongClick和Button1的一个(视图V)采用alertdialog查找数据。我得到的线空指针异常
TextView的stax1 =(的TextView)findViewById(R.id.xxx);
这是有道理的唯一事情是我有对话框中寻找alerthelp时,它应该从它附带的活动期待。我该如何解决这个问题?

  lay1.setOnLongClickListener(新OnLongClickListener(){
        公共布尔onLongClick(视图V)
        {            LayoutInflater myLayout = LayoutInflater.from(上下文);
            查看dialogView = myLayout.inflate(R.layout.alerthelp,NULL);
            AlertDialog.Builder alertDialogBu​​ilder =新AlertDialog.Builder(
            背景);
            AlertDialog alertDialog = alertDialogBu​​ilder.create();
            alertDialogBu​​ilder.setView(dialogView);
            alertDialog.show();
            按钮按钮1 =(按钮)dialogView.findViewById(R.id.button1);
            button1.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                TextView的stax1 =(的TextView)findViewById(R.id.xxx);
                。字符串SAX1 = stax1.getText()的toString();
                双SX1 = Double.parseDouble(SAX1);                TextView的textviewlay1 =(的TextView)findViewById(R.id.m1ab);
                。字符串stringl1 = textviewlay1.getText()的toString();
                 双doubl1 = Double.parseDouble(stringl1);                TextView的textviewp1 =(的TextView)findViewById(R.id.inputPrice);
                。字符串stringp1 = textviewp1.getText()的toString();
                双INTP1 = Double.parseDouble(stringp1);                双resultl1 = INTP1 - (doubl1 * SX1);
                INT precision21t = 100; //保留4位
                resultl1 = Math.floor(resultl1 * precision21t +.5)/ precision21t;
                textViewtotalproduct.setText(将String.valueOf(resultl1));            }
        });
            按钮按钮2 =(按钮)dialogView.findViewById(R.id.button2);
            button2.setOnClickListener(新OnClickListener(){            公共无效的onClick(视图v){
                TextView的textviewlay1 =(的TextView)findViewById(R.id.m1ab);
                。字符串stringl1 = textviewlay1.getText()的toString();
                双doubl1 = Double.parseDouble(stringl1);                TextView的textviewp1 =(的TextView)findViewById(R.id.inputPrice);
                。字符串stringp1 = textviewp1.getText()的toString();
                双INTP1 = Double.parseDouble(stringp1);                双resultl1 = INTP1 - doubl1;
                INT precision21t = 100; //保留4位
                resultl1 = Math.floor(resultl1 * precision21t +.5)/ precision21t;
                textViewtotalproduct.setText(将String.valueOf(resultl1));
                }
                });            按钮按钮3 =(按钮)dialogView.findViewById(R.id.button3);
            button3.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                TextView的SS1 =(的TextView)findViewById(R.id.s2);
                ss1.setText(ST);            }
        });
        按钮将Button4 =(按钮)dialogView.findViewById(R.id.button4);
        button4.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            TextView的SS1 =(的TextView)findViewById(R.id.s2);
                ss1.setText();
            }
        });        按钮button5 =(按钮)dialogView.findViewById(R.id.button5);
        button5.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                意图mainIntent =新意图(xxxActivity.this,SettingsActivity.class);
                MenuView1Activity.this.startActivity(mainIntent);
                MenuView1Activity.this.finish();
            }
        });        按钮button6 =(按钮)dialogView.findViewById(R.id.button6);
        button6.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                意图mainIntent =新意图(xxxActivity.this,SettingsActivity.class);
                xxxActivity.this.startActivity(mainIntent);
                xxxActivity.this.finish();
            }
        });
        返回false;
        }});


解决方案

看来你要加载从静态的TextView文本。也许是应EDITTEXT?

  TextView的stax1 =(TextView中)dialogView.findViewById(R.id.xxx);
。字符串SAX1 = stax1.getText()的toString();

编辑:

试试这个code:

  lay1.setOnLongClickListener(新OnLongClickListener(){
        公共布尔onLongClick(视图V)
        {            LayoutInflater myLayout = LayoutInflater.from(上下文);
            最后查看dialogView = myLayout.inflate(R.layout.alerthelp,NULL);
            AlertDialog.Builder alertDialogBu​​ilder =新AlertDialog.Builder(
            背景);
            alertDialogBu​​ilder.setView(dialogView);            AlertDialog alertDialog = alertDialogBu​​ilder.create();
            按钮按钮1 =(按钮)alertDialog.findViewById(R.id.button1);
            button1.setOnClickListener(新OnClickListener(){            公共无效的onClick(视图v){
                TextView的stax1 =(TextView中)dialogView.findViewById(R.id.xxx);
                。字符串SAX1 = stax1.getText()的toString();
                双SX1 = Double.parseDouble(SAX1);
                TextView的textviewlay1 =(TextView中)dialogView.findViewById(R.id.m1ab);
                。字符串stringl1 = textviewlay1.getText()的toString();
                 双doubl1 = Double.parseDouble(stringl1);
                TextView的textviewp1 =(TextView中)dialogView.findViewById(R.id.inputPrice);
                。字符串stringp1 = textviewp1.getText()的toString();
                双INTP1 = Double.parseDouble(stringp1);
                双resultl1 = INTP1 - (doubl1 * SX1);
                INT precision21t = 100; //保留4位
                resultl1 = Math.floor(resultl1 * precision21t +.5)/ precision21t;
                textViewtotalproduct.setText(将String.valueOf(resultl1));            }
        }); 返回false;
        }});

Im trying to get an AlertDialog to inflate from a customview. I think I know what the problem is but I dont know how to fix it. I have a (View V) on LongClick and Button1 is using "alertdialog" to find data. I'm getting a NullPointer Exception on line TextView stax1=(TextView)findViewById(R.id.xxx); The only thing that makes sense is I have the Dialog looking in "alerthelp" when it should looking in the activity it came from. How do I fix this?

        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;   




        }});

解决方案

It seems you are trying to load text from static TextView. May be it should be EditText?

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

EDITED:

Try this code:

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;  




        }});

这篇关于使用LayoutInflater和AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆