簡體   English   中英

DialogFragment:button.setOnClickListener引發NullPointerException

[英]DialogFragment: button.setOnClickListener throws NullPointerException

我有一個帶有這樣的按鈕的活動:

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

您必須引用根目錄按鈕的根視圖,其中按鈕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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM