从另一个调用FragmentDialog

时间:2015-03-19 05:21:17

标签: android android-dialogfragment dialogfragment

在我的Android项目中,我需要从第一个对话框调用我的第二个对话框,这两个对话框都是Dialog Fragment。我的代码似乎是正确的,因为我从我的活动中做同样的事情来调用第一个对话框但是对于第二个对话框它不会那样工作。

代码:

public class ProjectDialogFragment extends DialogFragment {

    public FragmentManager fManager = getFragmentManager();
    public CPDialogFragment cpDF = new CPDialogFragment();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup     container, Bundle savedInstanceState) {

        View view =     inflater.inflate(R.layout.project_dialogfragment,container,false);
       getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        Button btn_CreateProject =     (Button)view.findViewById(R.id.btn_CreateProject);
        btn_CreateProject.setOnClickListener(PFOnClickListener);
        Button btn_ProjectsList =     (Button)view.findViewById(R.id.btn_ProjectsList);
        btn_ProjectsList.setOnClickListener(PFOnClickListener);
        return view;
    }

    private final View.OnClickListener PFOnClickListener = new     View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId())
            {
                case R.id.btn_CreateProject:
                    Log.e("CP","CreateProject Clicked");

                    cpDF.show(fManager,"Create Project Dialog Fragment");
                    break;
                case R.id.btn_ProjectsList:
                      Log.e("CP","ProjectList Clicked");
                    break;
                default:
                    break;
            }
        }
    };
}

但它在此行抛出零点异常:

 cpDF.show(fManager,"Create Project Dialog Fragment");

1 个答案:

答案 0 :(得分:0)

通过将全局参数移动到本地,我的问题解决了:

首先我尝试创建方法:

  //prepare to open create project dialog fragment
    public void openCreateProjectDialogFragment() {
        FragmentManager fManager = getFragmentManager();
        CPDialogFragment cpDF = new CPDialogFragment();
        cpDF.show(fManager,"Create Project Dialog Fragment");
    }

然后在:

中使用它
 private final View.OnClickListener PFOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId())
            {
                case R.id.btn_CreateProject:
                    openCreateProjectDialogFragment();
                    dismiss();
                break;
                case R.id.btn_ProjectsList:
                    openProjectsListDialogFragment();
                    dismiss();
                    break;
                default:
                    break;
            }
        }
    };
}