为了账号安全,请及时绑定邮箱和手机立即绑定

如何修复在空对象引用上调用虚拟方法“android.view.View androidx

如何修复在空对象引用上调用虚拟方法“android.view.View androidx

繁星点点滴滴 2023-12-10 09:45:08
我想在选择日期后在文本视图中设置日期,但它在我评论的行中抛出错误,如何修复它?在这里创建视图public class MoonFragment extends Fragment {static {    System.loadLibrary("native-lib");}@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {    View view = inflater.inflate(R.layout.activity_moon,            container, false);    final TextView moon_ra = (TextView) view.findViewById(R.id.moon_ra);    Button button = (Button) view.findViewById(R.id.moon_btn);    button.setOnClickListener(new View.OnClickListener()    {        @Override        public void onClick(View v)        {            moon_ra.setText(Double.toString(Aries(0.2)));        }    });    EditText moon_date = (EditText) view.findViewById(R.id.moon_date);    moon_date.setOnClickListener(new View.OnClickListener()    {        @Override        public void onClick(View v) {            DialogFragment newFragment = new SelectDateFragment(); // It throws error here            newFragment.show(getFragmentManager(), "DatePicker");        }    });    return view;   // return inflater.inflate(R.layout.activity_moon, container, false);}public native double Aries (double d);在这里,我参加了选择日期的课程,它在第二行中抛出错误public static class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {    TextView moon_ra = (TextView)getActivity(). findViewById(R.id.moon_ra); // and here    @Override    public Dialog onCreateDialog(Bundle savedInstanceState) {        final Calendar calendar = Calendar.getInstance();        int yy = calendar.get(Calendar.YEAR);        int mm = calendar.get(Calendar.MONTH);        int dd = calendar.get(Calendar.DAY_OF_MONTH);        return new DatePickerDialog(getActivity(), this, yy, mm, dd);    }也许还有其他方法可以在文本视图中设置日期?
查看完整描述

2 回答

?
慕森卡

TA贡献1806条经验 获得超8个赞

显然#getActivity() 在附加片段之前为空。


您应该尝试将赋值代码放在#onCreateDialog()方法中:


TextView moon_ra; 


@Override

public Dialog onCreateDialog(Bundle savedInstanceState) {

    final Calendar calendar = Calendar.getInstance();

    int yy = calendar.get(Calendar.YEAR);

    int mm = calendar.get(Calendar.MONTH);

    int dd = calendar.get(Calendar.DAY_OF_MONTH);


    moon_ra = (TextView)getActivity().findViewById(R.id.moon_ra);


    return new DatePickerDialog(getActivity(), this, yy, mm, dd);

}


查看完整回答
反对 回复 2023-12-10
?
慕丝7291255

TA贡献1859条经验 获得超6个赞

您可以使用从到onActivityResult()传递数据,如下所示。SelectDateFragmentMoonFragment


SelectDateFragment如下所示在您的内部调用MoonFragment。


moon_date.setOnClickListener(new View.OnClickListener(){

    @Override

    public void onClick(View v) {

        DialogFragment newFragment = new SelectDateFragment();

        newFragment.setTargetFragment(this, 111); // add this line

        newFragment.show(getFragmentManager(), "DatePicker");

    }

});

发送数据从SelectDateFragment到MoonFragment新日期设置时


 public void populateSetDate(int year, int month, int day) {

     String newDate = month+"/"+day+"/"+year;


     Intent intent = new Intent();

     intent.putStringExtra("date", newDate);

     getTargetFragment().onActivityResult(getTargetRequestCode(), 111, intent);

}

要接收数据,请使用onActivityResult您的MoonFragment


public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 111) {

        String newDate = data.getStringExtra("date");

        // here you can update your textview

         moon_ra.setText(newDate); // make sure your moon_ra textview is decleared globally

    }

}


查看完整回答
反对 回复 2023-12-10
  • 2 回答
  • 0 关注
  • 112 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信