Search code examples
androiddialogtextviewandroid-custom-viewlayout-inflater

Dynamically update text within custom layout Dialog, NPE - Android


I'm trying to create a series of dialog menus that you can go through in Android. (Basically mocking up a USSD interaction in Android). Each dialog box is a textview menu of numbered choices, an EditText view for the user to enter their number choice, and two buttons, Cancel and Send (to progress to the next step).

While I can get my first main menu dialog to show up as I'd want, I'm having a lot of trouble getting my dialog text to dynamically update. When I try to call the TextView to set its text, it keeps giving me an NPE, and I'm not sure why!!

Here is the initial code in my activity (the dialog pops up immediately upon starting)

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

}

And here is the layout for the generic dialog view (called main menu right now, but will renamed to be generic if I can get this to work). The TextView doesn't contain any text right now, since I want it to be dynamically put in:

<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" />

When I run this, I get this error:

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)

Nothing I've tried is working, so any help would be appreciated! Thank you!


Solution

  • Try this:

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