2011-10-11 51 views
0

我正在尝试编写一个自定义对话框,该对话框需要用户的名称。我得到一个“OnClickListener无法解析为类型 - 类型View中的方法setOnClickListener(View.OnClickListener)不适用于Eclipse中的参数(新的OnClickListener(){})”错误。任何人都知道我在做什么错了?OnClick监听器对于自定义对话框的问题

这里是我的代码:

public void getName(){ 

     Dialog dialog = new Dialog(main.this); 
     dialog.setContentView(R.layout.customdialog); 
     dialog.setTitle("New Game"); 
     dialog.setCancelable(true); 
     //there are a lot of settings, for dialog, check them all out! 
     final EditText inputBox = new EditText(this); 

     //set up text 
     final TextView text = (TextView) dialog.findViewById(R.id.TextView01); 
     text.setText("Enter Your Name..."); 


     //set up button 
     final Button button = (Button) dialog.findViewById(R.id.namebutton); 
     button.setOnClickListener(new OnClickListener() { 
     public void onClick() { 
      String str = inputBox.getText().toString(); 
      setName(str); 
      } 
     }); 
     //now that the dialog is set up, it's time to show it  
     dialog.show(); 
    } 

回答

1

我猜你导入的错误OnClickListener。请确保您有:中

import android.view.View.OnClickListener; 

代替

import android.content.DialogInterface.OnClickListener; 
3

您可能只需要改变这个

button.setOnClickListener(new OnClickListener() { 

这个

button.setOnClickListener(new View.OnClickListener() { 

编辑 - 种组合的我们的答案,也确保你正在导入t正如克里斯蒂安所说的那样,他是正确的。