Я пытаюсь передать данные из DialogFragment
, где пользователи выбирают из TimePicker
, и он должен передать данные в действие через interface
.Я хочу установить текст кнопки mondayStart
с данными из interface
Но эти коды просто дают мне исключение NullPointerException:
AddSchedule.java
public class AddSchedule extends AppCompatActivity implements TimePicker.DialogListener{
TimePicker timepicker = new TimePicker();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_schedule);
findViewById(R.id.mondayStart).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timePicker.show(getSupportFragmentManager(), "time picker");
}
});
}
@Override
public void setTexts(String time) {
mondayStart.setText(time);
}
}
TimePicker.java
public class TimePicker extends DialogFragment {
String time;
DialogListener listener;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
TimePickerDialog timePickerDialog = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
@SuppressLint("DefaultLocale")
@Override
public void onTimeSet(android.widget.TimePicker view, int hourOfDay, int minute) {
time = String.format("%02d:%02d", hourOfDay, minute);
listener.setTexts(String.valueOf(hourOfDay));
}
},12, 0, false);
timePickerDialog.show();
return timePickerDialog;
}
@Override
public void onAttachFragment(Fragment childFragment) {
super.onAttachFragment(childFragment);
try {
listener = (DialogListener) childFragment;
} catch (ClassCastException e) {
throw new ClassCastException(childFragment.toString() + "implement listener");
}
}
public interface DialogListener{
void setTexts(String time);
}
}
это просто дает мне эти вещи в logcat
03-06 08:20:41.434 7579-7579/com.example.benedict.attendanceapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.benedict.attendanceapp, PID: 7579
java.lang.NullPointerException
at com.example.benedict.attendanceapp.TimePicker.onCreateDialog(TimePicker.java:39)
at android.support.v4.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:330)
at android.support.v4.app.Fragment.performGetLayoutInflater(Fragment.java:1308)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)