2013-05-29 3 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

В вашем логарифме? 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.

Смежные вопросы