• Android中findViewById()获取EditText 空指针问题


     因为EditText editText = (EditText)layout.findViewById(R.id.input_content);是从Dialog对话框布局layout中寻找ID为input_content的子元素
       EditText editText = (EditText)this.findViewById(R.id.input_content);// 空指针错误    因为是从this对象即当前ListActivity的布局List_view.xml中寻找ID为input_content的子元素EditText,而我们在List_view.xml布局文件中并无定义此元素故空指针错误 ,并且我们的目的并不是这样。
    只要理解findViewById就行了。

    简单点讲:把你的findViewById改成Dialog的findViewById默认的是你的Activity的findViewById,这个肯定取不到EditText里面的值,肯定是空的。

    void addbtn() {
            ImageButton ib_add = (ImageButton) findViewById(R.id.addpoint);
            ib_add.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    LayoutInflater inflater = getLayoutInflater();
                    final View layout = inflater.inflate(R.layout.add_dialog,
                            (ViewGroup) findViewById(R.id.dialog));
                    new AlertDialog.Builder(PointMgrActivity.this)
                            .setTitle("请添加节点")
                            .setView(layout)
                            .setPositiveButton("确定",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog,
                                                int which) {
                                            EditText et = (EditText)layout.findViewById(R.id.addID);
                                            String gID = et.getText().toString();
                                            
                                           
                                            Log.i(PointMgrActivity.ACTIVITY_TAG,
                                                    gID);
                                            
                                              String pStatu="OFF";
                                            Toast.makeText(
                                              PointMgrActivity.this,
                                             gID +
                                              "   " +pStatu,
                                              Toast.LENGTH_LONG).show();
                                             
                                        }
                                    })
                            .setNegativeButton("取消",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog,
                                                int which) {
    
                                        }
                                    }).show();
                }
    
            });
    
        }
  • 相关阅读:
    ECSHOP 2.5.1 二次开发文档【文件结构说明和数据库表分析】
    ECSHOP后台左侧添加菜单栏
    PHPstudy 2018 集成环境项目配置虚拟域名访问
    微擎左侧模块业务菜单修改
    ThinkPHP5使用阿里云OSS图片上传
    ThinkPHP5使用PHPExcel实现数据导出功能
    ThinkPHP5生成二维码图片与另一张背景图片进行合成
    原生PHP连接MySQL数据库
    数组的冒泡排序
    【原创诗歌】读仓央嘉措(下)
  • 原文地址:https://www.cnblogs.com/NeilLing/p/4040056.html
Copyright © 2020-2023  润新知