我已经使用对话框片段在我的应用程序中显示一个对话框。但是,当我点击显示应用程序强制关闭对话框的按钮时。
代码
public class ShareDialogFragment extends DialogFragment implements View.OnClickListener {
private View view;
private Bundle bundle, bundle1;
private Button btsharecancel;
private Button btnonpocketusers;
private Button btpocketusers;
private int position;
private FragmentTransaction fragmentTransaction;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.share_dialog, container, false);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, 0);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initialize();
}
private void initialize() {
btsharecancel = (Button) getActivity().findViewById(R.id.bt_share_cancel);
btnonpocketusers = (Button) getActivity().findViewById(R.id.bt_non_pocket_users);
btpocketusers = (Button) getActivity().findViewById(R.id.bt_pocket_users);
btnonpocketusers.setOnClickListener(this);
}
@Override
public void onClick(View view) {
position = bundle.getInt("position");
bundle.putInt("position", position);
Log.e("Position Value", "" + position);
switch (view.getId()) {
case R.id.bt_non_pocket_users:
ShareDocsEmailFragment shareDocsEmailFragment = new ShareDocsEmailFragment();
shareDocsEmailFragment.setArguments(bundle1);
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frag_container, shareDocsEmailFragment, "shareDocsEmail");
fragmentTransaction.addToBackStack("shareDocsEmailBack").commit();
break;
}
}
}
在此代码存在时显示对话框之前
public class ShareDialogFragment extends DialogFragment implements View.OnClickListener {
private View view;
private Bundle bundle, bundle1;
private Button btsharecancel;
private Button btnonpocketusers;
private Button btpocketusers;
private int position;
private FragmentTransaction fragmentTransaction;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.share_dialog, container, false);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, 0);
}
}
但是当我在按钮上添加听众时(在对话框中)应用关闭
logcat的
07-31 15:54:57.819 12273-12273/example.com.pocketdocs E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at example.com.pocketdocs.ShareDialog.ShareDialogFragment.initialize(ShareDialogFragment.java:57)
at example.com.pocketdocs.ShareDialog.ShareDialogFragment.onActivityCreated(ShareDialogFragment.java:47)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:852)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1039)
at android.app.BackStackRecord.run(BackStackRecord.java:635)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1405)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:430)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:4787)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
我用小哈塞尔解决了答案。对话片段唯一要记住的就是使用这个
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.share_dialog, container, false);
return view;
}
button = (Button) view.findViewById(R.id.button);
INSTEAD OF
button = (Button) getActivity().findViewById(R.id.button);