【问题标题】:Using LayoutInflater and AlertDialog使用 LayoutInflater 和 AlertDialog
【发布时间】:2013-06-27 08:31:12
【问题描述】:

我试图让 AlertDialog 从自定义视图中膨胀。我想我知道问题是什么,但我不知道如何解决它。我在 LongClick 上有一个(视图 V),而 Button1 正在使用“alertdialog”来查找数据。我在线收到 NullPointer 异常 TextView stax1=(TextView)findViewById(R.id.xxx); 唯一有意义的是,当它应该查看它来自的活动时,我让对话框查看“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;   




        }});

【问题讨论】:

  • 我将展示我更新的代码。现在 Lay1 onClick 让我的屏幕稍微变暗了。很奇怪。
  • 有人帮忙请...
  • 我要删除这个问题伙计们。我非常需要这个。我希望当我为您的帮助加分时,它不会因为我删除了问题而消失。我将再次发布它,也许会问一些不同的问题。
  • 好吧,我想我不能删除它。

标签: android nullpointerexception android-alertdialog layout-inflater


【解决方案1】:

您似乎正在尝试从静态 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 执行计算。该 textView 来自另一个活动,该活动以 EditText 开始,但通过意图和共享首选项保存在当前页面上,该页面打开一个对话框以进行计算或其他一些选择。
  • 我完全按照你说的做了,我又在 button1.setOnClickListener(new OnClickListener(){ Button 1 is defintely on XML alerthelp 上得到了 NullPointerException。我不明白。我什至删除了我的代码并复制了你写的东西......同样的事情
  • 我不会错过那些新手成长的烦恼。
【解决方案2】:

改变

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

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

希望这会有所帮助。

【讨论】:

  • 对话框打开后我应该把所有的TextView都改了吧?
  • 是的。所有处于膨胀视图中的文本视图
  • 现在我没有从 LongClick 得到任何东西。它什么都不做
  • 您是否尝试将 Button button1 = (Button) alertDialog.findViewById(R.id.button1); 更改为 Button button1 = (Button) dialogView.findViewById(R.id.button1); ? @SmulianJulian
  • 当我这样做时,lay1 的 OnClick 没有响应。警报对话框打不开,不是我已经打开了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
相关资源
最近更新 更多