日期从日期picker对话框没有得到设置在文本视图的弹出式android



我的问题是在onDateSet方法中,我如何从我的textview字段上的datepicker对话框中检索选定的日期,这是一个嵌入在弹出窗口上的活动。我得到nullpointer异常:((TextView) getActivity () .findViewById (R.id.spinner_date)) . settext("日期"+年);

DatePickerFragment.java

public 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);
    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {
     Log.w("DatePicker", "Date = " + year);
  ((TextView) getActivity().findViewById(R.id.spinner_date)).setText("Date"+year);
}

}

在MainActivity.java

     public void createVideoEvent(View arg0) {
    LayoutInflater layoutInflater
            = (LayoutInflater) getBaseContext()
            .getSystemService(LAYOUT_INFLATER_SERVICE);
    popupView = layoutInflater.inflate(R.layout.activity_event_create,          null);
    final PopupWindow popupWindow = new PopupWindow(
            popupView,
           400,
            LayoutParams.WRAP_CONTENT);
    View new_event_button = (View) findViewById(R.id.button);
    popupWindow.showAsDropDown(new_event_button);
   dateSpinner = (TextView) popupView.findViewById(R.id.spinner_date);
    View.OnTouchListener Spinner_OnTouch = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                showDatePickerDialog(v);
            }
            return true;
        }
    };
    View.OnKeyListener Spinner_OnKey = new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
                showDatePickerDialog(v);
                return true;
            } else {
                return false;
            }
        }
    };
    dateSpinner.setOnTouchListener(Spinner_OnTouch);
    dateSpinner.setOnKeyListener(Spinner_OnKey);
     SimpleDateFormat sdf = new SimpleDateFormat("cccc, MMMM dd, yyyy", Locale.US);
    dateSpinner.setText(sdf.format(myCalendar.getTime()));
}
public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "Date");
}

首先,您需要在DialogFragment中创建一个接口,并将数据发送到该接口。接下来,您需要在活动中实现该接口的侦听器。在该侦听器中,您将设置文本。不应该有任何问题,但如果有让我知道,我会尽力帮助。

DatePickerFragment首先你想在这个类中创建一个接口,然后在活动中实现它。

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
import java.util.Calendar;

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 year, int month, int day) {
        mCallbacks.setTextDate(year, month, day);
    }
    /**
     * Interface to communicate to the parent activity (MainActivity.java)
     */
    private FragmentCallbacks mCallbacks;
    public interface FragmentCallbacks {
        void setTextDate(int year, int month, int day);
    }
    @Override
    public void onAttach(Activity activity) { // Attach it to the activity
        super.onAttach(activity);
        try {
            mCallbacks = (FragmentCallbacks) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException("Activity must implement Fragment One.");
        }
    }
    @Override
    public void onDetach() { // Remove the listener
        super.onDetach();
        mCallbacks = null;
    }
}

MainActivity确保调用实现了DatePickerFragment。扩展类后的fragmentcallback !

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
// Make sure you implement the listener in the DatePickerFragment()
public class MainActivity extends AppCompatActivity implements DatePickerFragment.FragmentCallbacks {
    private TextView dateSpinner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Removed the touch listeners for this example to make it simple.
        // Show dialog on start for this example
        showDatePickerDialog();
        dateSpinner = (TextView) findViewById(R.id.spinner_date);
    }
    // Changed it to getSupportFragmentManager(),
    // You might need to change yours back to getFragmentManager()
    public void showDatePickerDialog() {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getSupportFragmentManager(), "Date");
    }
    // The interface listener created in the DatePickerFragment()
    @Override
    public void setTextDate(int year, int month, int day) {
        dateSpinner.setText("Date Year: " + year);
    }
}

最新更新