2013-04-27 49 views
0

所以我有一個時間選擇器全部設置好了,它在我的應用程序中打開得很好。我可以選擇時間,點擊'完成',它就像它應該的那樣關閉。我現在想要輸入時間並將其顯示在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開發人員和編程一般,所以一些非常詳細的,愚蠢的,足以換一個孩子理解的答案將不勝感激!!!!

在此先感謝!

回答

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); 
} 
+0

嘗試你的建議後,所以我仍然得到同樣的NullPointerException異常錯誤...玩了一個小時,仍然沒有得到它! – 2013-04-28 19:27:00

+0

究竟哪一行是例外?您可以使用新的StackTrace更新您的問題修改代碼和LogCat – iTech 2013-04-28 21:59:18