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

标签 android android-alertdialog uicomponents

我正在尝试构建带有 View 的警报对话框并访问 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();
}

日志布局.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。谁能帮我知道我哪里错了!

谢谢

最佳答案

这似乎有效。玩得开心!

        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();

关于android - 检索 AlertDialog 组件时出现问题 - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5754105/

相关文章:

android - 如何使用 Android 在不使用 toast(Alter 对话框)的情况下在设置错误消息中制作编辑文本框?

android - Android 4.1 上的 AlertDialog 问题

jsf-2 - 访问 JSF Managed Bean 中的动态 UIComponent

javascript - 建议将哪些 UI 框架与 Backbone.js 一起使用?

android - 如何使用 Hilt 将安全参数参数注入(inject) View 模型?

java - 如何获取对话框大小?

android - 使用 WorkManager 更新 Room 中的数据库条目

android - 如何解决 Flutter Agora RTC 显示错误代码 110

android - 更改 Sweet alert 中的按钮样式

jsf - EL 中的 #{component} 到底是什么?