• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

Neil_Ling

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

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

        });

    }

 

posted on 2014-10-21 12:34  Neil_Ling  阅读(774)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3