0

我有一个在按下按钮时启动时间选择器对话框的活动。但是,当按下按钮时,对话框不会出现并且应用程序崩溃。我在下面显示了我所有的相关代码。

如果有人可以帮助我解决这个问题,将不胜感激。

感谢您的帮助。

活动代码

    public class SecondActivity extends Activity implements TimePickerFragment.OnCompleteListener 

    {


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);


        }

        public void showTimePickerDialog(View v) {
            DialogFragment newFragment = new TimePickerFragment();
            newFragment.show(getFragmentManager(), "timePicker");
        }





           public void onComplete(String time) {

                Button btn = (Button) findViewById(R.id.start_time_button);
                btn.setText(time);

            }
        }

对话片段

public class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {



@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user

    String time =Integer.toString(hourOfDay)+" : " +Integer.toString(minute);
    this.mListener.onComplete(time);

}


        public static interface OnCompleteListener {
            public abstract void onComplete(String time);
}

            private OnCompleteListener mListener;

            // make sure the Activity implemented it
            public void onAttach(Activity activity) {
                try {
                    this.mListener = (OnCompleteListener)activity;
    }
                catch (final ClassCastException e) {
                    throw new ClassCastException(activity.toString() + " must implement OnCompleteListener");
    }
}


}

XML

<LinearLayout 
    android:id="@+id/timeinput"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:layout_marginTop="20dp"
    android:orientation="horizontal" >


        <Button
        android:id="@+id/start_time_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:layout_marginLeft="30dp"
        android:padding="10dp"
        android:onClick="showTimePickerDialog"  />


 </LinearLayout>

日志猫

02-28 18:16:11.429: D/AbsListView(23796): Get MotionRecognitionManager
02-28 18:16:11.479: D/libEGL(23796): loaded /system/lib/egl/libEGL_mali.so
02-28 18:16:11.484: D/libEGL(23796): loaded /system/lib/egl/libGLESv1_CM_mali.so
02-28 18:16:11.489: D/libEGL(23796): loaded /system/lib/egl/libGLESv2_mali.so
02-28 18:16:11.494: D/(23796): Device driver API match
02-28 18:16:11.494: D/(23796): Device driver API version: 10
02-28 18:16:11.494: D/(23796): User space API version: 10 
02-28 18:16:11.494: D/(23796): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
02-28 18:16:11.524: D/OpenGLRenderer(23796): Enabling debug mode 0
02-28 18:16:13.054: E/SensorManager(23796): thread start
02-28 18:16:13.054: D/SensorManager(23796): registerListener :: handle = 0  name= LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41cb53d8
02-28 18:16:13.204: E/SpannableStringBuilder(23796): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
02-28 18:16:13.204: E/SpannableStringBuilder(23796): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
02-28 18:16:14.224: D/AndroidRuntime(23796): Shutting down VM
02-28 18:16:14.224: W/dalvikvm(23796): threadid=1: thread exiting with uncaught exception (group=0x410122a0)
02-28 18:16:14.229: E/AndroidRuntime(23796): FATAL EXCEPTION: main
02-28 18:16:14.229: E/AndroidRuntime(23796): android.app.SuperNotCalledException: Fragment TimePickerFragment{41cc4718 #0 timePicker} did not call through to super.onAttach()
02-28 18:16:14.229: E/AndroidRuntime(23796):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:789)
02-28 18:16:14.229: E/AndroidRuntime(23796):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
02-28 18:16:14.229: E/AndroidRuntime(23796):    at android.app.BackStackRecord.run(BackStackRecord.java:635)
02-28 18:16:14.229: E/AndroidRuntime(23796):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
02-28 18:16:14.229: E/AndroidRuntime(23796):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
02-28 18:16:14.229: E/AndroidRuntime(23796):    at android.os.Handler.handleCallback(Handler.java:615)
02-28 18:16:14.229: E/AndroidRuntime(23796):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-28 18:16:14.229: E/AndroidRuntime(23796):    at android.os.Looper.loop(Looper.java:137)
02-28 18:16:14.229: E/AndroidRuntime(23796):    at android.app.ActivityThread.main(ActivityThread.java:4898)
02-28 18:16:14.229: E/AndroidRuntime(23796):    at java.lang.reflect.Method.invokeNative(Native Method)
02-28 18:16:14.229: E/AndroidRuntime(23796):    at java.lang.reflect.Method.invoke(Method.java:511)
02-28 18:16:14.229: E/AndroidRuntime(23796):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-28 18:16:14.229: E/AndroidRuntime(23796):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-28 18:16:14.229: E/AndroidRuntime(23796):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

6

尝试向 onAttach() 方法添加超级调用:

public void onAttach(Activity activity) {
    super.onAttach(activity);
于 2013-02-28T18:22:52.067 回答