0

我需要更改对话框的外观,因此我决定使用QustomDialogQustomDialogBu​​ilder找不到项目

我通过setCustomView使用自定义布局,但是当我处理onClickClick的PositiveButton时我找不到我的布局的任何元素。

布局包括在EditText上:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <EditText 
     android:id="@+id/setServer" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/server_url" /> 

</LinearLayout> 

监听器是:

QustomDialogBuilder builder = new QustomDialogBuilder(this); 
/* Some customization */ 
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        String url = ((EditText) findViewById(R.id.setServer)).getText().toString(); 
        if (Utilities.isUrl(url)) { 
         PreferenceHelper.setRestUrl(getApplicationContext(), url); 
        } else { 
         Toast.makeText(getApplicationContext(), "Invalid URL", Toast.LENGTH_SHORT); 
        } 
       } 
      }); 

但是,当我按下 “OK” 我得到java.lang.ClassCastException: com.android.internal.view.menu.ActionMenuItemView cannot be cast to android.widget.EditText

我该如何解决这个问题?

回答

1

可能是findViewById方法正在寻找错误的布局(在活动布局而不是对话框中)。

你可以尝试创建与对话最终的变数时,会显示它:

final AlertDialog dialog = builder.show(); 

然后在findViewById使用它:

String url = ((EditText) dialog.findViewById(R.id.setServer)).getText().toString(); 
+0

我已经找到了相同的解决方案调试代码在几分钟前! 谢谢! – Luca 2014-09-02 10:18:09