i have this portion of code for a dialogfragment
public class DialogOne extends DialogFragment {
private Button b;
public DialogOne() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_one, container);
getDialog().setTitle("Titolo del dialog");
b = (Button)getDialog().findViewById(R.button.ok);
b.setOnclickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getDialog().dismiss();
}
});
return view;
}
so when i press the button, dialogfragment must close. When i running the app, i've a NullPoinerException on the row contains new OnClickListener. How can i fix it?
> 02-28 21:05:44.623: E/AndroidRuntime(8487): FATAL EXCEPTION: main
02-28 21:05:44.623: E/AndroidRuntime(8487): java.lang.NullPointerException
02-28 21:05:44.623: E/AndroidRuntime(8487): at com.example.dialogs.DialogOne.onActivityCreated(DialogOne.java:37)
02-28 21:05:44.623: E/AndroidRuntime(8487): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1468)
02-28 21:05:44.623: E/AndroidRuntime(8487): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
02-28 21:05:44.623: E/AndroidRuntime(8487): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
02-28 21:05:44.623: E/AndroidRuntime(8487): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
02-28 21:05:44.623: E/AndroidRuntime(8487): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
02-28 21:05:44.623: E/AndroidRuntime(8487): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
02-28 21:05:44.623: E/AndroidRuntime(8487): at android.os.Handler.handleCallback(Handler.java:725)
02-28 21:05:44.623: E/AndroidRuntime(8487): at android.os.Handler.dispatchMessage(Handler.java:92)
02-28 21:05:44.623: E/AndroidRuntime(8487): at android.os.Looper.loop(Looper.java:137)
02-28 21:05:44.623: E/AndroidRuntime(8487): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-28 21:05:44.623: E/AndroidRuntime(8487): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 21:05:44.623: E/AndroidRuntime(8487): at java.lang.reflect.Method.invoke(Method.java:511)
02-28 21:05:44.623: E/AndroidRuntime(8487): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-28 21:05:44.623: E/AndroidRuntime(8487): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-28 21:05:44.623: E/AndroidRuntime(8487): at dalvik.system.NativeStart.main(Native Method)
I think you want to replace
b = (Button)getDialog().findViewById(R.button.ok);
with
b = (Button) view.findViewById(R.button.ok);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With