【问题标题】:Dynamically update text within custom layout Dialog, NPE - Android在自定义布局对话框中动态更新文本,NPE - Android
【发布时间】:2016-03-25 18:03:03
【问题描述】:

我正在尝试创建一系列可以在 Android 中使用的对话框菜单。 (基本上模拟了 Android 中的 USSD 交互)。每个对话框都是一个带有编号选项的文本视图菜单、一个供用户输入数字选项的 EditText 视图,以及两个按钮,取消和发送(进入下一步)。

虽然我可以根据需要显示我的第一个主菜单对话框,但我在让我的对话框文本动态更新时遇到了很多麻烦。当我尝试调用 TextView 来设置它的文本时,它一直给我一个 NPE,我不知道为什么!!

这是我的活动中的初始代码(对话框在启动时立即弹出)

TextView txtContent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);
}

protected void onStart() {
    super.onStart();
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    // Get the layout inflater
    LayoutInflater inflater = this.getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.main_menu_dialog, null))
            // Add action buttons
            .setPositiveButton(R.string.send, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // Send number to next dialog
                    FirstTimeUse();
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // End session
                }
            });
    AlertDialog dialog = builder.create();
    TextView txtContent = (TextView)findViewById(R.id.main_menu_options);
    txtContent.setText(R.string.MainMenuText);
    dialog.show();

}

这是通用对话框视图的布局(现在称为主菜单,但如果我可以让它工作,将重命名为通用)。 TextView 现在不包含任何文本,因为我希望它被动态放入:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
    android:id="@+id/main_menu_options"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="4dp" />
<EditText
    android:id="@+id/choice"
    android:inputType="textShortMessage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="16dp" />

当我运行这个时,我得到这个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)

我尝试过的所有方法都不起作用,因此我们将不胜感激!谢谢!

【问题讨论】:

    标签: android dialog textview android-custom-view layout-inflater


    【解决方案1】:

    试试这个:

    View dialogView = inflater.inflate(R.layout.main_menu_dialog, null);
    builder.setView(dialogView)
    ...
    TextView txtContent = (TextView) dialogView.findViewById(R.id.main_menu_options);
    txtContent.setText(R.string.MainMenuText);
    

    【讨论】:

    • 嘿,成功了!太感谢了!!你碰巧知道为什么会这样吗?单独创建一个视图与我上面直接来自 Android Dialog 教程的内容有什么区别?
    • 您正在搜索活动(或片段)视图层次结构中的文本视图。但是对话框有自己的层次视图(甚至是单独的窗口来显示),所以你应该在后者中搜索它。
    • 知道了。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多