ダイアログフラグメントのコードのこの部分があります
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;
}
ボタンを押すと、ダイアログフラグメントを閉じる必要があります。アプリを実行すると、行に NullPoinerException があり、新しい OnClickListener が含まれています。どうすれば修正できますか?
> 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)