Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set onclicklistener on a button inside a DialogFragment

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)
like image 724
giozh Avatar asked Feb 17 '23 08:02

giozh


1 Answers

I think you want to replace

b = (Button)getDialog().findViewById(R.button.ok);

with

b = (Button) view.findViewById(R.button.ok);

like image 107
Tyler MacDonell Avatar answered Feb 19 '23 22:02

Tyler MacDonell