自定义setMessage对话框TextView无法强制转换为java.lang.CharSequence

时间:2014-10-14 19:59:24

标签: java android android-layout android-dialog

我正在尝试从我的dialogBox打印一条消息,此消息来自API,我假装在我为其设置的TextView中打印,下面是我正在尝试做的事情和错误日志:

close = new AlertDialog.Builder(LembrarSenha.this);

close.setCancelable(true);

close.setIcon(R.drawable.ic_launcher);

String msg = mNovaSenhaRest.getNovaSenha(value).getMsg();

LayoutInflater inflater = getLayoutInflater();
View view=inflater.inflate(R.layout.titlebar, null);

TextView informativo = (TextView) view.findViewById(R.id.informacao);
informativo.setText(msg); // Line 149

close.setMessage((CharSequence) informativo);

close.setView(view);

日志:

java.lang.ClassCastException: android.widget.TextView cannot be cast to java.lang.CharSequence
10-14 16:54:25.048  22956-22956/br.com. W/System.err﹕ at br.com..LembrarSenha$asyncTask$1.run(LembrarSenha.java:149)

(LembrarSenha.java:149)=“informativo.setText(msg);”

1 个答案:

答案 0 :(得分:2)

不应该将TextView初始化为:

TextView informativo = (TextView) view.findViewById(R.id.informacao);

因为您正在尝试向对话框添加自定义视图。您的视图会膨胀,但在初始化TextView时,您不会在自定义布局中查找它。