Chris
Chris

Reputation: 6311

DialogFragment: button.setOnClickListener throws NullPointerException

I have an Activity with a button like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="onButtonClickListener"
    android:text="Show Fragment" />
</RelativeLayout>

And my onButtonClickListener method from xml is(here I create the DialogFragment and set onClickListener for a clear button from dialog):

 public void onButtonClickListener(View view) {
    FragmentManager fm = getFragmentManager();
    MyDialogFragment dialogFragment = new MyDialogFragment ();
    dialogFragment.show(fm, "Sample Fragment");

    final EditText messageText = (EditText) findViewById(R.id.editText);
    Button clearButton = (Button) findViewById(R.id.button_clear);

   clearButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            messageText.setText(null);
        }
    });
}

My problem is that when DialogFragment shows it throws NullPointerException:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.cristi.dialogfragmenttest2.MainActivity.onButtonClickListener(MainActivity.java:51)

At this line of code: clearButton.setOnClickListener(new View.OnClickListener() {

My xml for DialogFragment is:

<?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="match_parent">

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:maxLength="200" />

<Button
    android:id="@+id/button_clear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Clear"
    android:layout_below="@+id/editText"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

What could be the problem with the setOnClickListener? Why does it says that my button from DialogFragment is null?

Any reference or advice is welcome! Thanks in advance!

Edit:

-MyDialogFragment code:

public class MyDialogFragment extends DialogFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.dialog_fragment, container, false);
    getDialog().setTitle("Enter message");
    return rootView;
}

Upvotes: 1

Views: 725

Answers (1)

dieter_h
dieter_h

Reputation: 2727

You must have refernece to the root view where is your button button_clear, then call rootView.findViewById(...) to get the button reference... Solution:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.dialog_fragment, container, false);
        getDialog().setTitle("Enter message");
     Button clearButton = (Button) rootView.findViewById(R.id.button_clear);

       clearButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                messageText.setText(null);
            }
        });
        return rootView;
    }

Upvotes: 1

Related Questions