-2

通过在 dateonset () 方法中执行此操作,我以多种方式尝试了此 button.set(),尽管 editText4 包含设置按钮的有效文本,但我在那里得到了一个空指针异常。我尝试了共享首选项的路线,因为我怀疑 edittext4 是空的。最后,我尝试使用此方法从视图中初始化 Onclick 方法中的 Datebtn。在这里,我没有得到空指针异常,但是 switch case 内的 Datebtn.setText() 没有 settext 选定的日期值 & Datebtn 文本继续仅显示在 onCreateView () 方法中设置的当前日期。我想我错过了一些微不足道的东西。

public class AFragment extends Fragment implements OnClickListener {

    private DataManipulator dh;
    public Button           Datebtn;
    private Calendar        mCalen;
    private int             day;
    private int             month;
    private int             year;
    public String           myEditText4;

    @Override
    public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
        View view = inflater.inflate( R.layout.afragment, container, false );
        Button b1 = (Button)view.findViewById( R.id.Button01 );
        b1.setOnClickListener( this );
        Button Datebtn = (Button)view.findViewById( R.id.datepickerbutton );
        mCalen = Calendar.getInstance();
        day = mCalen.get( Calendar.DAY_OF_MONTH );
        month = mCalen.get( Calendar.MONTH );
        year = mCalen.get( Calendar.YEAR );
        Datebtn.setText( day + " / " + (month + 1) + " / " + year );
        myEditText4 = (String)Datebtn.getText();
        Datebtn.setOnClickListener( this );
        return view;
    }

    @SuppressLint( "CutPasteId" )
    @Override
    public void onClick( View view ) {
        Context context = view.getContext();
        Button Datebtn = (Button)view.findViewById( R.id.datepickerbutton );

        switch( view.getId() ) {
            case (R.id.datepickerbutton):
                DatePickerFragment newDateFragment = new DatePickerFragment();
                newDateFragment.setStyle( 1, 1 );
                newDateFragment.show( getFragmentManager(), "DatePicker" );
                Datebtn.setText( myEditText4 );
                break;

            case (R.id.Button01):
                View editText1 = (EditText)getView().findViewById( R.id.ppl );
                View editText2 = (EditText)getView().findViewById( R.id.litres );
                View editText3 = (EditText)getView().findViewById( R.id.odo );
                String myEditText1 = ((TextView)editText1).getText().toString();
                String myEditText2 = ((TextView)editText2).getText().toString();
                String myEditText3 = ((TextView)editText3).getText().toString();
                dh = new DataManipulator( context );
                if( myEditText1 != "" & myEditText2 != "" & myEditText3 != "" & myEditText4 != "" ) {
                    dh.insert( myEditText1, myEditText2, myEditText3, myEditText4 );
                    Toast.makeText( context.getApplicationContext(), "Submit Successful!!!", Toast.LENGTH_LONG ).show();
                    EditText text1 = (EditText)getView().findViewById( R.id.ppl );
                    text1.setText( "" );
                    EditText text2 = (EditText)getView().findViewById( R.id.litres );
                    text2.setText( "" );
                    EditText text3 = (EditText)getView().findViewById( R.id.odo );
                    text3.setText( "" );
                } else {
                    Toast.makeText( context.getApplicationContext(), "Values can not be empty!!!", Toast.LENGTH_LONG ).show();
                }
                break;
        }
    }

    @SuppressLint( "ValidFragment" )
    public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog( Bundle savedInstanceState ) {
            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 selectedYear, int selectedMonth, int selectedDay ) {
            int year = selectedYear;
            int month = selectedMonth;
            int day = selectedDay;
            myEditText4 = (day + " / " + (month + 1) + " / " + year);

            return;
        }
    }

}
4

1 回答 1

0

在调试所有变量的状态后,我能够找出问题所在。这是解决方案
1) 当我在 onDateSet 方法中执行 Datebtn 时,我实际上是在尝试从 Datepicker 视图中获取 Datebtn 对象。Datebtn 不在此片段中,因此它给出空指针异常
2) 然后我尝试使用片段管理器对象和 findFragmentByTag("AFragment") 从 onDateSet 内部获取父片段。这也返回一个空指针,因为没有为 AFragment 布局 xml 或 AFragment 类定义“AFragment”标记
3) 然后我尝试使用 findFragmentByID (R.id.fragment_container) 从片段管理器对象中获取有效的 AFragment 对象。在这一步,我在 DatePickerFragment 类中获得了一个有效的 AFragment 对象句柄。然后我可以在 DatePickerFragment 类的 AFragment 类中的 Datebtn 上执行 setText

这是 DatePickerFragment 的 onDateSet 的工作代码的外观

    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        int year = selectedYear;
        int month = selectedMonth;
        int day = selectedDay;
        myEditText4 = (day + " / " + (month + 1) + " / " + year);
        FragmentManager fm = this.getFragmentManager();
        Fragment parFragment = fm.findFragmentById(R.id.fragment_container);
        Button dbtn = (Button) parFragment.getView().findViewById(R.id.datepickerbutton);
        dbtn.setText(myEditText4);
    return;
    }
于 2013-06-26T15:33:48.087 回答