无法使用接口将数据从DialogFragment传递到Activity

时间:2019-03-06 00:23:07

标签: android android-dialogfragment android-timepicker

我正在尝试从DialogFragment传递数据,其中用户从TimePicker中进行选择,它应该通过interface将数据传递给Activity。 我想要发生的是使用来自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)

1 个答案:

答案 0 :(得分:0)

在您的TimePicker片段中,您想覆盖onAttach(Context)而不是onAttachFragment()。附加上下文后,请使用instanceof测试它是否为DialogListener,如果是,则将其强制转换并存储在listener变量中。为了安全起见,当您使用侦听器时,请检查它是否为空。