Search code examples
androidandroid-linearlayoutsettextfindviewbyidr.id

ERROR on dialog creation in Android Studio


I am getting this error java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

I tried changing the setContentView after setting the Text but with no luck. I can't find what I am doing wrong even thought I searched.

Here is my dialog creation. The error comes up when I set the text from the textview.

public void forgetDialog() {
        final Dialog dialog = new Dialog(this); // Context, this, etc.

        dialog.setTitle(R.string.dialog_title2);

        //  dialog.setOnDismissListener(dialog.dismiss(););
        dialog.setContentView(R.layout.dialog);
        TextView txv = (TextView)findViewById(R.id.dialog_info);
         txv.setText("");
        txv.setText("You forgot to add the ingredients!");

        dialog.show();
        btn_dialog = (Button) dialog.findViewById(R.id.dialog_ok) ;
        btn_dialog.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
    }

And here is the xml I am using

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/dialog_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="@string/dialog_text"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/dialog_info">



        <Button
            android:id="@+id/dialog_ok"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.50"
            android:background="@color/dialog_ok_bgcolor"
            android:text="Ok, got it"/>
    </LinearLayout>
</RelativeLayout>

Solution

  • First Step
    Write two separate file xml, one for the layout of the activity (activity.xml), one for the layout of the dialog (dialog.xml). Make sure that your dialog.xml contains the TextView and the Button that you need.

    Second Step
    Declare
    Button btn_dialog; TextView txv;

    before the code that I put in the next step.

    Third Step
    Try this code, I tried it and it worked.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        forgetDialog();
    }
    
    public void forgetDialog() {
        final Dialog dialog = new Dialog(this); // Context, this, etc.
        dialog.setTitle("Title");
    
        //  dialog.setOnDismissListener(dialog.dismiss(););
        dialog.setContentView(R.layout.dialog);
        txv = (TextView) dialog.findViewById(R.id.dialog_info);
        txv.setText("");
        txv.setText("You forgot to add the ingredients!");
    
        dialog.show();
    
        btn_dialog = (Button) dialog.findViewById(R.id.dialog_ok);
        btn_dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
    }
    

    I think that the error were:
    1) the management of the layouts;
    2) you have forgotten to tell in which View you want to find the id of the TextView.