onCreateDialog里面创建Dialog,组件空指针异常解决

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:30   1460   0
ContractedBlock.gif ExpandedBlockStart.gif View Code
protected Dialog onCreateDialog(int id) {
switch(id)
{
case NEWMESSAGEDIALOG:
LayoutInflater lf
=LayoutInflater.from(this);
final View alertView=lf.inflate(R.layout.newmessagedialog, null);
final ImageButton newMessageIB=(ImageButton) alertView.findViewById(R.id.newmessageSearch);
final EditText newMessageNumber=(EditText) alertView.findViewById(R.id.newmessageNumber);
final EditText newMessageNeiRong=(EditText) alertView.findViewById(R.id.newmessageEditText);
final AlertDialog.Builder newMessageDialog=new AlertDialog.Builder(this);

newMessageDialog.setView(alertView);
newMessageDialog.setTitle(
this.getResources().getString(R.string.newMessage_Title));

newMessageDialog.setPositiveButton(
this.getResources().getString(R.string.sentbutton), new DialogInterface.OnClickListener(){

@Override
public void onClick(DialogInterface arg0, int arg1) {
if(newMessageNumber.getText().toString().equals(""))
{
Toast.makeText(InBoxActivity.
this, R.string.numberIsNull, Toast.LENGTH_SHORT).show();
return;
}
else if(newMessageNeiRong.getText().toString().equals(""))
{
Toast.makeText(InBoxActivity.
this, R.string.messageIsNull, Toast.LENGTH_SHORT).show();
return;
}
// MyAsyncTask task=new MyAsyncTask();
}});
return newMessageDialog.create();
}
return super.onCreateDialog(id);
}



之前用了this.findViewById应该改为final ImageButton newMessageIB
=(ImageButton) alertView.findViewById(R.id.newmessageSearch);

转载于:https://www.cnblogs.com/helloandroid/articles/2144350.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP