1

我想使用自定義時間選擇器,然後從片段運行選擇器。這是時間選擇器對話框(TimePickerFragment.java)的代碼:從片段加載timePickerDialog

package com.ravit.android.aroundtheclock; 

import android.app.Dialog; 
import android.app.DialogFragment; 
import android.app.Fragment; 
import android.app.TimePickerDialog; 
import android.os.Bundle; 
import android.text.format.DateFormat; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TimePicker; 

import java.util.Calendar; 

/** 
* A simple {@link Fragment} subclass. 
*/ 
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener { 


    public TimePickerFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the current time as the default values for the picker 

     final Calendar calendar = Calendar.getInstance(); 
     int hour = calendar.get(Calendar.HOUR_OF_DAY); 
     int minute = calendar.get(Calendar.MINUTE); 

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

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_time_picker, container, false); 
    } 

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

,這是從onCreateView片段的代碼,顯示時間選擇器(WorkdayFragment.java):

mButtonEnd = (Button)v.findViewById(R.id.end_workday); 
mButtonEnd.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     TimePickerFragment dialog = new TimePickerFragment(); 
     dialog.show(getActivity().getFragmentManager(), DIALOG_TIME); 
    } 
}); 

此是按下按鈕時出現的錯誤。好像在加載視圖中的問題:

10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: FATAL EXCEPTION: main 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: Process: com.ravit.android.aroundtheclock, PID: 20325 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: android.util.AndroidRuntimeException: requestFeature() must be called before adding content 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at com.android.internal.policy.PhoneWindow.requestFeature(PhoneWindow.java:317) 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at com.android.internal.app.AlertController.installContent(AlertController.java:231) 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at android.app.AlertDialog.onCreate(AlertDialog.java:423) 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at android.app.Dialog.dispatchOnCreate(Dialog.java:394) 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at android.app.Dialog.show(Dialog.java:295) 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at android.app.DialogFragment.onStart(DialogFragment.java:499) 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at android.app.Fragment.performStart(Fragment.java:2244) 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1002) 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148) 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at android.app.BackStackRecord.run(BackStackRecord.java:793) 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535) 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482) 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at android.os.Handler.handleCallback(Handler.java:739) 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:95) 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:148) 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5417) 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method) 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我的猜測是有是因爲當我和日期選擇器代替它,然後一切都沒有錯誤運行時間選擇器編寫代碼的方式有問題。

我試圖解決requestFeature()錯誤根據這個主題的其他答案沒有進展。

任何有關如何從這裏繼續的建議將是非常有益的。 謝謝!

+0

我明白爲什麼要重寫DialogFragment的onCreateView方法。您已經返回計時器選取器對話框。 –

+0

你是對的,問題解決了:) – Ravit

回答

2

嘿,首先你需要刪除你的timepicker片段的onCreateView。 我也可以在我的例子中找到分享。

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) {...} 

所以你TimerPickerFragment應該如下

package com.ravit.android.aroundtheclock; 

import android.app.Dialog; 
import android.app.DialogFragment; 
import android.app.Fragment; 
import android.app.TimePickerDialog; 
import android.os.Bundle; 
import android.text.format.DateFormat; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TimePicker; 

import java.util.Calendar; 

/** 
* A simple {@link Fragment} subclass. 
*/ 
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener { 


    public TimePickerFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the current time as the default values for the picker 

     final Calendar calendar = Calendar.getInstance(); 
     int hour = calendar.get(Calendar.HOUR_OF_DAY); 
     int minute = calendar.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 
    } 
} 

注意:如果您的應用程序支持的Android版本低於3.0,是 確保您撥打getSupportFragmentManager()來獲取一個實例 的片段管理器。還要確保您的活動顯示 時間選擇器擴展了FragmentActivity而不是標準的 活動類。

mButtonEnd = (Button)v.findViewById(R.id.end_workday); 
mButtonEnd.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     DialogFragment newFragment = new TimePickerFragment(); 
     //newFragment.show(getActivity().getFragmentManager(), DIALOG_TIME); 
     newFragment.show(getSupportFragmentManager(), DIALOG_TIME); 
     // if you are using the nested fragment then user the 
     //newFragment.show(getChildFragmentManager(), DIALOG_TIME); 
    } 
}); 

讓我知道如果你有任何問題。謝謝

+0

我確實有一個問題,你爲什麼使用getSupportFragmentManager而不是getActivity()。getFragmentManager()? – Ravit

+0

如果您的應用支持Android版本低於3.0,請確保您調用getSupportFragmentManager()來獲取FragmentManager的實例。其他明智的是,如果你使用默認的片段管理器, –

+0

非常感謝你的幫助:) – Ravit