从外部类设置edittext值时出错

时间:2017-02-09 11:10:27

标签: android

我正在尝试将所选日期值设置为edittext。我收到了以下错误

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'

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

4 个答案:

答案 0 :(得分:0)

已更改

  static TextView tvStartVisit;


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

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

答案 1 :(得分:0)

您可以在对话框方法中传递该textview的对象。

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

答案 2 :(得分:0)

因为tvStartVisitnull。你还没有约束View

onCreateDialog中,请务必致电ButterKnife.bind(this, view);(或以您的图书馆的绑定方式为准)。

或者您可以在TextView创建之前传递您的日期。

答案 3 :(得分:0)

试试这个

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