检索 AlertDialog 组件时出现问题 - android
我正在尝试构建一个带有视图的警报对话框并访问 TextView 以设置其文本值。这是代码:
private void ShowLogDialog() {
AlertDialog ad = new AlertDialog.Builder(this).create();
ad.setIcon(R.drawable.icon);
ad.setTitle("Ultimate Logs");
ad.setView(LayoutInflater.from(this).inflate(R.layout.log_layout, null));
TextView text = (TextView)ad.findViewById(R.id.logTextView_log); // RETURNS NULL
//Log.i(TAG, "Into ShowLogDialog : GoT TextView = " + text);
//String logsText = "";
//text.setText(logsText);
ad.setButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
}
log_layout.xml
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:scrollbars="vertical" android:scrollbarAlwaysDrawVerticalTrack="true" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_height="wrap_content" android:text="TextView" android:id="@+id/**logTextView_log**" android:layout_width="wrap_content"></TextView>
</LinearLayout>
为什么我无法访问 TextView ?它返回 null 并抛出 NullPointerException。我无法直接使用 findBiewById 访问,所以我使用 ad.findViewById,但我只收到 null 。谁能帮我知道我哪里出错了!
谢谢
I am tring to build an alertdialog with a view and access a TextView to set its text value. Here's the code :
private void ShowLogDialog() {
AlertDialog ad = new AlertDialog.Builder(this).create();
ad.setIcon(R.drawable.icon);
ad.setTitle("Ultimate Logs");
ad.setView(LayoutInflater.from(this).inflate(R.layout.log_layout, null));
TextView text = (TextView)ad.findViewById(R.id.logTextView_log); // RETURNS NULL
//Log.i(TAG, "Into ShowLogDialog : GoT TextView = " + text);
//String logsText = "";
//text.setText(logsText);
ad.setButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
}
log_layout.xml
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:scrollbars="vertical" android:scrollbarAlwaysDrawVerticalTrack="true" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_height="wrap_content" android:text="TextView" android:id="@+id/**logTextView_log**" android:layout_width="wrap_content"></TextView>
</LinearLayout>
Why an I not able to access TextView ? it returns null and throws NullPointerException. I can't access directly using findBiewById, so am using ad.findViewById, yet I receive it null only. Can anyone help me know where am I going wrong !
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这似乎有效。玩得开心!
This seems to work. Have fun!
这是什么意思..?
android:id="@+id/**logTextView_log**
** 意味着..?
what is meaning of this..?
android:id="@+id/**logTextView_log**
** means..?
为什么不首先将 LayoutInflater.from(this).inflate(R.layout.log_layout, null) 返回到
View
变量? (以下代码未经测试)Why not returning
LayoutInflater.from(this).inflate(R.layout.log_layout, null)
to aView
variable first ? (The below code is untested)