我想将DatePickerDialog中的选定日期返回到我的片段。 我测试它,我只进入构建日志这个错误: E / ViewRootImpl:sendUserActionEvent()mView == null
在Samsung Galaxy S6 Edge(7.0)上测试我的代码
出现对话框,我可以选择一个日期,点击确定但是没有将结果发送到我的TextView ...我忘记了一些细节吗?
My Fragment我调用DatePickerDialog:
package at.marcelklein.drautaxi;
import android.app.DatePickerDialog;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import java.text.DateFormat;
import java.util.Calendar;
/**
* Created by Marcel on 10.09.2017.
*/
public class FourthFragment extends Fragment implements DatePickerDialog.OnDateSetListener {
View myView;
TextView textView;
Button btnDayStart, btnDayEnd;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.fourth_layout, container, false);
btnDayStart = (Button) myView.findViewById(R.id.button_daystart);
btnDayEnd = (Button) myView.findViewById(R.id.button_dayend);
textView = (TextView) myView.findViewById(R.id.textView12);
btnDayStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment datePicker = new DatePickerFragment();
FragmentTransaction ft = ((FragmentActivity)getActivity()).getSupportFragmentManager().beginTransaction();
datePicker.show(ft, "datepicker");
}
});
btnDayEnd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//showDialog();
}
});
return myView;
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String currentDateString = DateFormat.getDateInstance(DateFormat.FULL).format(c.getTime());
textView.setText(currentDateString);
}
}

DatePickerDialog片段:
package at.marcelklein.drautaxi;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import java.util.Calendar;
public class DatePickerFragment extends DialogFragment {
DatePickerDialog.OnDateSetListener onDateSet;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
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(), onDateSet, year, month, day);
}
}

提前致谢!
答案 0 :(得分:0)
您忘记将片段指定为侦听器。像这样的听众添加它
btnDayStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment datePicker = new DatePickerFragment();
datePicker.onDateSet = FourthFragment.this
FragmentTransaction ft = ((FragmentActivity)getActivity()).getSupportFragmentManager().beginTransaction();
datePicker.show(ft, "datepicker");
}
});
答案 1 :(得分:0)
我将代码更改为: DatePickerDialog
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
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);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
TextView tv = (TextView) getActivity().findViewById(R.id.textView12);
DecimalFormat df = new DecimalFormat("00");
tv.setText("Gewähltes Datum:");
tv.setText(tv.getText() + "\nJahr: " + year);
tv.setText(tv.getText() + "\nMonat: " + df.format(month));
tv.setText(tv.getText() + "\nTag: " + df.format(day));
}
-----------我的片段我称之为 DatePickerDialog :
public class FourthFragment extends Fragment {
View myView;
public TextView textView;
Button btnDayStart, btnDayEnd;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.fourth_layout, container, false);
btnDayStart = (Button) myView.findViewById(R.id.button_daystart);
btnDayEnd = (Button) myView.findViewById(R.id.button_dayend);
textView = (TextView) myView.findViewById(R.id.textView12);
btnDayStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment datePicker = new DatePickerFragment();
datePicker.show(getFragmentManager(), "date Picker");
}
});
return myView;
}
现在我直接从DatePickerDialog更改TextView。 我认为那不是最好的解决方案......
我想要的是一个范围日期选择器,如" TimeSquare DatePicker"。 但我不能在过去的时间里选择TimeSquare DatePicker"。
有什么建议吗?