Java DialogFragment:button.setOnClickListener抛出NullPointerException
我有一个带有如下按钮的活动:Java DialogFragment:button.setOnClickListener抛出NullPointerException,java,android,xml,button,android-dialogfragment,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"> &l
<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>
我的问题是,当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(新视图.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>
您必须有根视图的引用,其中是您的按钮
button\u 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;
}
放置
MyDialogFragment
@Rustam的代码我添加了一个带有MyDialogFragmenttry(按钮)dialogFragment.findViewById(R.id.Button\u clear)代码的编辑
@Rustam一个家伙给了我正确的答案。谢谢你的时间。谢谢你的回答,它起了作用,我意识到我的代码从OOP的角度来看是不正确的。当然,DialogFragment中的按钮必须在DialogFragment类中声明。再次感谢!
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;
}
@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;
}