2013-05-29 55 views
1

我想創建對話框使用片段下面這個[鏈接] [1],NullPointerException異常當調用對話片段

這裏我的代碼爲對話片段

public class AlertFragmentDialog extends DialogFragment 
{ 
    public static final int DIALOG_EXIT_ALERT = 1; 

    private static final String KEY_TITLE = "titles"; 
    private static final String KEY_DIALOG = "dialogtype"; 

    public static DialogFragment newInstance(int _title,int _dialogType){ 
     AlertFragmentDialog frag = new AlertFragmentDialog(); 
     Bundle args = new Bundle(); 
     args.putInt(KEY_TITLE, _title); 
     args.putInt(KEY_DIALOG, _dialogType); 
     frag.setArguments(args);   
     return null; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     switch(getArguments().getInt(KEY_DIALOG)) 
     { 
      case DIALOG_EXIT_ALERT : 
       return new AlertDialog.Builder(getActivity()).setTitle(getArguments().getInt(KEY_TITLE)) 
         .setPositiveButton(R.string.global_yes,new DialogInterface.OnClickListener() 
         { 
          public void onClick(DialogInterface dialog, int whichButton) 
          { 
           ((MainActivity)getActivity()).doYesConfirmationClick(); 
          } 
         }) 
         .setNegativeButton(R.string.global_no, new DialogInterface.OnClickListener() 
         { 
          public void onClick(DialogInterface dialog, int whichButton) 
          { 
           ((MainActivity)getActivity()).doNoConfirmationClick(); 
          } 
         }).create(); 
     } 
     return null; 
    } 
} 

並使用此代碼

叫它
FragmentManager fm = getSupportFragmentManager(); 
     DialogFragment alertDialog = AlertFragmentDialog.newInstance(R.string.global_exit_alert,AlertFragmentDialog.DIALOG_EXIT_ALERT); 
     alertDialog.show(fm, "dialog"); 

但是,當我運行我有我的模擬器上的錯誤NullPointerException。 alertDialog.show(fm,「dialog」); 請幫助我不知道我的代碼有什麼問題..

+0

什麼在你的logcat? NullpointerExceptions向您顯示發生錯誤的行。檢查這一行 – B770

回答

1
public static DialogFragment newInstance(int _title,int _dialogType){ 
    AlertFragmentDialog frag = new AlertFragmentDialog(); 
    Bundle args = new Bundle(); 
    args.putInt(KEY_TITLE, _title); 
    args.putInt(KEY_DIALOG, _dialogType); 
    frag.setArguments(args);   
    return frag; // you were returning null 
} 
2

您的newInstance()返回null。將其更改爲返回frag

相關問題