2015-03-19 76 views
0

調用FragmentDialog在我的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"); 
+0

你可以嘗試'公共FragmentManager fManager = getChildFragmentManager();' – Xcihnegn 2015-03-19 08:24:42

回答

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; 
      } 
     } 
    }; 
}