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

我想在选择日期后在文本视图中设置日期,但它在我评论的行中抛出错误,如何修复它?


在这里创建视图


public class MoonFragment extends Fragment {

static {

    System.loadLibrary("native-lib");


}

@Nullable

@Override

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

    }


也许还有其他方法可以在文本视图中设置日期?


繁星点点滴滴
浏览 120回答 2
2回答

慕森卡

显然#getActivity() 在附加片段之前为空。您应该尝试将赋值代码放在#onCreateDialog()方法中:TextView moon_ra; @Overridepublic 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);}

慕丝7291255

您可以使用从到onActivityResult()传递数据,如下所示。SelectDateFragmentMoonFragmentSelectDateFragment如下所示在您的内部调用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您的MoonFragmentpublic 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    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java