无法显示用户输入的时间

时间:2013-04-27 23:25:48

标签: java android android-activity datepicker fragment

所以我设置了一个时间选择器,它在我的应用程序中打开就好了。我可以选择时间,点击'完成',它就像它应该的那样关闭。我现在想要花时间输入并在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 Dev和一般编程都很陌生,因此非常感谢一些非常详细,愚蠢到足以让孩子理解的答案!!!!

提前致谢!

1 个答案:

答案 0 :(得分:0)

覆盖onActivityCreated()并在那里初始化您的文字,例如:

private TextView view;

@Override
public void onActivityCreated (Bundle savedInstanceState){
  view = (TextView) getView().findViewById(R.id.timeOfWorkoutTextView);
}

然后修改onTimeSet以使用view,而不是调用getView()

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  TextView textView = (TextView)view.findViewById(R.id.timeOfWorkoutTextView);
}