DialogFragment:button.setOnClickListener抛出NullPointerException

时间:2015-08-22 09:32:29

标签: java android xml button android-dialogfragment

我有一个带有这样一个按钮的活动:

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

我的xml中的onButtonClickListener方法是(这里我创建了DialogFragment并在对话框中设置onClickListener以获得一个清除​​按钮):

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

我的问题是,当DialogFragment显示它会抛出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)

在这行代码中:clearButton.setOnClickListener(new View.OnClickListener() {

我的DialogFragment xml是:

<?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>

setOnClickListener有什么问题?为什么说DialogFragment的按钮是空的?

欢迎任何参考或建议!提前谢谢!

修改

-MyDialogFragment代码:

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

1 个答案:

答案 0 :(得分:1)

您必须引用根视图,按钮button_clear在哪里,然后调用rootView.findViewById(...)获取按钮参考... 解决方案:

@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;
    }