2017-02-09 14 views
-2

編集日に選択した日付の値を設定しようとしています。私は、エラー、次の取得しています外部からedittext値を設定中にエラーが発生しました

のjava.lang.NullPointerException:nullのオブジェクト参照

public static class AddDataFragment extends BaseFragment implements { 
@BindView(R.id.tvStartVisit) 
    public TextView tvStartVisit; 

@Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_add_visit, container, false); 
     ButterKnife.bind(this, view); 
     setData(); 
     return view; 
    } 

@OnClick(R.id.tvStartVisit) 
    public void onStartVisitDateClicked(){ 
     DialogFragment newDialogFragment = new DatePickerFragment(); 
     newDialogFragment.show(getActivity().getSupportFragmentManager(), "datePicker"); 
    } 

    public static class DatePickerFragment extends DialogFragment 
      implements DatePickerDialog.OnDateSetListener { 

     @Override 
     public Dialog onCreateDialog(Bundle savedInstanceState) { 
      // Use the current date as the default date in the picker 
      final Calendar c = Calendar.getInstance(); 
      int year = c.get(Calendar.YEAR); 
      int month = c.get(Calendar.MONTH); 
      int day = c.get(Calendar.DAY_OF_MONTH); 

      return new DatePickerDialog(getActivity(), this, year, month, day); 
     } 

     public void onDateSet(DatePicker view, int year, int month, int day) { 
      AddVisitFragment mAddVisitFragmentForDatePicker = new AddDataFragment(); 
      mAddVisitFragmentForDatePicker.tvStartVisit.setText(year+"-"+month+"-"+day); 
     } 
    } 
} 

答えて

0
に '無効android.widget.TextView.setText(java.lang.CharSequence)' 仮想メソッドを呼び出すための試み

変更されました

static TextView tvStartVisit; 


    public void onDateSet(DatePicker view, int year, int month, int day) { 

     tvStartVisit.setText(year+"-"+month+"-"+day); 
    } 
0

ダイアログメソッドでそのテキストビューのオブジェクトを渡すことができます。

public static class AddDataFragment extends BaseFragment implements { 
     @BindView(R.id.tvStartVisit) 
     public TextView tvStartVisit; 

     @OnClick(R.id.tvStartVisit) 
     public void onStartVisitDateClicked(){ 
      DialogFragment newDialogFragment = new DatePickerFragment(tvStartVisit); 
      newDialogFragment.show(getActivity().getSupportFragmentManager(), "datePicker"); 
     } 

     public static class DatePickerFragment extends DialogFragment 
       implements DatePickerDialog.OnDateSetListener { 

      TextView tvStartVisit; 
      DatePickerFragment(TextView tvStartVisit) { 
       this.tvStartVisit = tvStartVisit; 
      } 

      @Override 
      public Dialog onCreateDialog(Bundle savedInstanceState) { 
       // Use the current date as the default date in the picker 
       final Calendar c = Calendar.getInstance(); 
       int year = c.get(Calendar.YEAR); 
       int month = c.get(Calendar.MONTH); 
       int day = c.get(Calendar.DAY_OF_MONTH); 

       return new DatePickerDialog(getActivity(), this, year, month, day); 
      } 

      public void onDateSet(DatePicker view, int year, int month, int day) { 
       AddVisitFragment mAddVisitFragmentForDatePicker = new AddDataFragment(); 
       tvStartVisit.setText(year+"-"+month+"-"+day); 
      } 
     } 
    } 
+0

取得エラーを試します。 } –

0

tvStartVisitnullあるためです。あなたはViewに縛られていません。

onCreateDialogには、ButterKnife.bind(this, view);(またはあなたのライブラリのバインド方法であるかどうか)を必ず確認してください。

TextViewが作成される前に日付を過ぎている可能性があります。

0

DatePickerFragment(のTextView tvStartVisit){ this.tvStartVisit = tvStartVisitでこの

public static class AddDataFragment extends BaseFragment 
        implements DatePickerDialog.OnDateSetListener { 
@BindView(R.id.tvStartVisit) 
public TextView tvStartVisit; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_add_visit, container, false); 
    ButterKnife.bind(this, view); 
    setData(); 
    return view; 
} 

@OnClick(R.id.tvStartVisit) 
public void onStartVisitDateClicked(){ 
    DialogFragment newDialogFragment = new DatePickerFragment(AddDataFragment.this); 
    newDialogFragment.show(getActivity().getSupportFragmentManager(), "datePicker"); 
} 

public static class DatePickerFragment extends DialogFragment { 
    DatePickerDialog.OnDateSetListener listener 
    public DatePickerFragment(DatePickerDialog.OnDateSetListener listener) { 
    this.listener = listerner; 
    } 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the current date as the default date in the picker 
     final Calendar c = Calendar.getInstance(); 
     int year = c.get(Calendar.YEAR); 
     int month = c.get(Calendar.MONTH); 
     int day = c.get(Calendar.DAY_OF_MONTH); 

     return new DatePickerDialog(getActivity(), listener, year, month, day); 
    } 
} 

@Override 
public void onDateSet(DatePicker view, int year, int month, int day) { 
     tvStartVisit.setText(year+"-"+month+"-"+day); 
    } 
} 
+0

DialogFragmentでエラーが発生しませんnewDialogFragment = new DatePickerFragment(AddDataFragment.this); –

+0

エラーの詳細を教えてください – arjun