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