0

사용자 정의 DialogFragment을 구현하려고합니다. 그러나 그것을 보여 주려고 할 때 나는 NullPointerException을 얻고 있습니다. 또한 내가 알았 듯이 onCreateDialog은 암묵적으로 호출되지 않습니다.
무엇이 잘못 되었나요? 나는 공식 설명서를 읽어하고 DialogFragment
다음은 사용자 정의 대화 상자 조각show 후 onCreateDialog가 호출됩니까?

public class UserInputDialogFragment extends DialogFragment { 
    InputDialogListener mListener; 
    private EditText mTextEdit; 

    public UserInputDialogFragment() { 
     super(); 
    } 

    // Use this instance of the interface to deliver action events 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     // Get the layout inflater 
     LayoutInflater inflater = getActivity().getLayoutInflater(); 

     // Inflate and set the layout for the dialog 
     // Pass null as the parent view because its going in the dialog layout 
     View mainView = inflater.inflate(R.layout.dialog_input, null); 
     builder.setView(mainView); 
     mTextEdit = (EditText) mainView.findViewById(R.id.user_input); 
     if (mTextEdit==null) { 
      Log.e("ERROR","Text edit is null"); 
     } 
     // Add action buttons 
     builder.setPositiveButton(R.string.ok_btn, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int id) { 
       mListener.onDialogPositiveClick(UserInputDialogFragment.this,mTextEdit.getText().toString()); 
      } 
     }); 
     builder.setNegativeButton(R.string.cancel_bnt, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       mListener.onDialogNegativeClick(UserInputDialogFragment.this,mTextEdit.getText().toString()); 
       UserInputDialogFragment.this.getDialog().cancel(); 
      } 
     }); 

     return builder.create(); 
    } 

    public interface InputDialogListener { 
     public void onDialogPositiveClick(DialogFragment dialog, String userInput); 

     public void onDialogNegativeClick(DialogFragment dialog, String userInput); 
    } 


    public void showAndAddHint(FragmentManager manager,String tag,String hint) { 
     this.onCreateDialog(null); 
     mTextEdit.setHint(hint); 
     this.show(manager,tag); 

    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     // Verify that the host activity implements the callback interface 
     try { 
      // Instantiate the NoticeDialogListener so we can send events to the host 
      mListener = (InputDialogListener) activity; 
     } catch (ClassCastException e) { 
      // The activity doesn't implement the interface, throw exception 
      throw new ClassCastException(activity.toString() 
        + " must implement InputDialogListener"); 
     } 
    } 
} 

내 코드는 내가 대화 이런 식으로 보여주기 위해 노력하고있는 모든 단계를 따라했습니다. 여기

UserInputDialogFragment userInputDialogFragment = new UserInputDialogFragment(); 
        userInputDialogFragment.showAndAddHint(getFragmentManager(),"Please enter phone number",task.phoneNumber); 

그리고는 NullPointerExceptionmTextEditnull입니다.

public void showAndAddHint(FragmentManager manager,String tag,String hint) { 
    this.onCreateDialog(null); 
    mTextEdit.setHint(hint); 
    this.show(manager,tag); 

} 

답변

2

showAndAddHint 메서드는 작성된대로 작동하지 않습니다. 당신이 대신해야 할 것은 :

1 - 당신이 지금하고있는 방식 그대로 show() 전화 - mHint = hint;

이 변수 멤버를 설정합니다.

3 - 생성 대화 상자의 멤버 변수 mHint을 읽고 편집 텍스트 힌트를 설정하는 데 사용하십시오.

onCreateDialog을 명시 적으로 호출하지 마십시오. show 메서드가 필요할 때이를 수행하기 때문에 명시 적으로 호출하지 마십시오.

+0

매력처럼 작동합니다. 고마워. 해피 홀리데이 및 새해 – atzcijhu