所以我已经设置了一个时间选择器,它在我的应用程序中打开得很好。我可以选择时间,点击“完成”,然后它会按预期关闭。我现在想花点时间在 XML 文件中定义的 TextView 中显示它。
我最接近这样做的是使用这行代码:
TextView textView = (TextView)getView().findViewById(R.id.timeOfWorkoutTextView);
这段代码至少在 Eclipse 中没有显示错误,但是每次我运行应用程序并点击“完成”按钮时,它都会引发错误并关闭应用程序。以下是 LogCat:
04-27 19:13:49.482: E/AndroidRuntime(11553): FATAL EXCEPTION: main
04-27 19:13:49.482: E/AndroidRuntime(11553): java.lang.NullPointerException
04-27 19:13:49.482: E/AndroidRuntime(11553): at com.example.test_project.TimePickerFragment.onTimeSet(TimePickerFragment.java:30)
04-27 19:13:49.482: E/AndroidRuntime(11553): at android.app.TimePickerDialog.tryNotifyTimeSet(TimePickerDialog.java:130)
04-27 19:13:49.482: E/AndroidRuntime(11553): at android.app.TimePickerDialog.onClick(TimePickerDialog.java:115)
04-27 19:13:49.482: E/AndroidRuntime(11553): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
04-27 19:13:49.482: E/AndroidRuntime(11553): at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 19:13:49.482: E/AndroidRuntime(11553): at android.os.Looper.loop(Looper.java:137)
04-27 19:13:49.482: E/AndroidRuntime(11553): at android.app.ActivityThread.main(ActivityThread.java:5059)
04-27 19:13:49.482: E/AndroidRuntime(11553): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 19:13:49.482: E/AndroidRuntime(11553): at java.lang.reflect.Method.invoke(Method.java:511)
04-27 19:13:49.482: E/AndroidRuntime(11553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
04-27 19:13:49.482: E/AndroidRuntime(11553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
04-27 19:13:49.482: E/AndroidRuntime(11553): at dalvik.system.NativeStart.main(Native Method)
04-27 19:13:51.060: I/Process(11553): Sending signal. PID: 11553 SIG: 9
我打算使用 StringBuilder 创建一个包含小时和分钟的字符串,然后像这样使用它: textView.setText(hourMinuteString);
这是我的所有代码:
package com.example.test_project;
import java.util.Calendar;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;
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) {
TextView textView = (TextView)getView().findViewById(R.id.timeOfWorkoutTextView);
}
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
}
}
我对 Android 开发和编程非常陌生,因此非常感谢一些非常详细、愚蠢到足以让孩子理解的答案!!!!
提前致谢!