检索 AlertDialog 组件时出现问题 - android

发布于 2024-11-02 13:42:29 字数 1464 浏览 4 评论 0原文

我正在尝试构建一个带有视图的警报对话框并访问 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

知足的幸福 2024-11-09 13:42:29

这似乎有效。玩得开心!

        AlertDialog.Builder builder= new AlertDialog.Builder(this);
        LayoutInflater inflater= getLayoutInflater();
        final View myView= inflater.inflate(R.layout.alert_dialog_text_entry, null);
        builder.setTitle("About");
        builder.setMessage("Test");
        builder.setView(myView);
        AlertDialog alert= builder.create();
        TextView stateful= (TextView)myView.findViewById(R.id.TextView01);
        stateful.setText("More Testing");
        Log.d(Utilities.TAG,stateful.toString());
        alert.show();

This seems to work. Have fun!

        AlertDialog.Builder builder= new AlertDialog.Builder(this);
        LayoutInflater inflater= getLayoutInflater();
        final View myView= inflater.inflate(R.layout.alert_dialog_text_entry, null);
        builder.setTitle("About");
        builder.setMessage("Test");
        builder.setView(myView);
        AlertDialog alert= builder.create();
        TextView stateful= (TextView)myView.findViewById(R.id.TextView01);
        stateful.setText("More Testing");
        Log.d(Utilities.TAG,stateful.toString());
        alert.show();
戒ㄋ 2024-11-09 13:42:29

这是什么意思..?

android:id="@+id/**logTextView_log**

** 意味着..?

what is meaning of this..?

android:id="@+id/**logTextView_log**

** means..?

中性美 2024-11-09 13:42:29

为什么不首先将 LayoutInflater.from(this).inflate(R.layout.log_layout, null) 返回到 View 变量? (以下代码未经测试)

    ad.setTitle("Ultimate Logs");
    View inflatedView = LayoutInflater.from(this).inflate(R.layout.log_layout, null);         
    TextView text = (TextView)inflatedView.findViewById(R.id.logTextView_log);  // RETURNS NULL
    Log.i(TAG, "Into ShowLogDialog : GoT TextView = " + text);
    String logsText = "";
    text.setText(logsText);
    ad.setView(inflatedView );
    ad.setButton("Ok", new DialogInterface.OnClickListener() {   

Why not returning LayoutInflater.from(this).inflate(R.layout.log_layout, null) to a View variable first ? (The below code is untested)

    ad.setTitle("Ultimate Logs");
    View inflatedView = LayoutInflater.from(this).inflate(R.layout.log_layout, null);         
    TextView text = (TextView)inflatedView.findViewById(R.id.logTextView_log);  // RETURNS NULL
    Log.i(TAG, "Into ShowLogDialog : GoT TextView = " + text);
    String logsText = "";
    text.setText(logsText);
    ad.setView(inflatedView );
    ad.setButton("Ok", new DialogInterface.OnClickListener() {   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文