我的问题是在onDateSet方法中,如何从textview字段上的datepicker对话框中检索所选日期,该字段是弹出窗口中嵌入的活动。我得到了nullpointer异常: ((TextView)getActivity()。findViewById(R.id.spinner_date))。setText(“Date”+ year);
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");
}
答案 0 :(得分:0)
首先,您要在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 确保在扩展课程后调用implements DatePickerFragment.FragmentCallbacks!
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);
}
}