0

我在一个android应用程序中有这个代码,当按下菜单项时它会显示一个对话框,它工作正常但是当我从对话框设置按钮“发送”的功能时,单击菜单项时它会崩溃。我找不到问题

 private void showdialog (String ap){
    final Dialog dialog = new Dialog(this);
    et =(EditText)dialog.findViewById (R.id.commentET);
    send=(Button)dialog.findViewById(R.id.send);
    send.setOnClickListener(sendOnClickListener);
    dialog.setTitle(ap);
    dialog.setContentView(R.layout.comment);
    System.getProperty("line.separator");
    dialog.setCancelable(true);
    dialog.show();
}

Button.OnClickListener sendOnClickListener
= new Button.OnClickListener(){



public void onClick(View arg0) {
  s=et.getText().toString();
  dialog.dismiss();
} 
};




public boolean onCreateOptionsMenu(Menu menu) {
   getMenuInflater().inflate(R.menu.aprobare_menu, menu);
   return true;
}

public boolean onOptionsItemSelected(MenuItem item) 
   {


      switch (item.getItemId())
      {
         case R.id.aproba:
             showdialog("Aprobat");
             return true;

         case R.id.respinge:
            showdialog("Respins");
             return true;

       default:
            return super.onOptionsItemSelected(item);
      }
      }    

};
4

2 回答 2

1

尝试更改调用 setContentView 的顺序:

private void showdialog (String ap){
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.comment);
et =(EditText)dialog.findViewById (R.id.commentET);
send=(Button)dialog.findViewById(R.id.send);
send.setOnClickListener(sendOnClickListener);
dialog.setTitle(ap);
System.getProperty("line.separator");
dialog.setCancelable(true);
dialog.show();
}

不过不确定。

于 2012-11-22T15:42:15.957 回答
1

在通过 id 查找任何视图之前contentView为您的对话框设置:

private void showdialog (String ap){
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.comment);// should be before findViewById
    et =(EditText)dialog.findViewById (R.id.commentET);
    send=(Button)dialog.findViewById(R.id.send);
    send.setOnClickListener(sendOnClickListener);
    dialog.setTitle(ap);

    System.getProperty("line.separator");
    dialog.setCancelable(true);
    dialog.show();
}
于 2012-11-22T15:42:17.180 回答