2

請幫我使用NumberPickerhttps://github.com/SimonVT/android-numberpicker。 我有片段,裏面有按鈕。點擊按鈕後,我想顯示我的號碼選擇器(作爲彈出窗口,而不是新的屏幕)。 所以我做了什麼: 我創建NumberPickerCustomDialog定製號碼選取器裏面的片段

public class NumberPickerCustomDialog extends DialogFragment { 
Context context; 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    // get context 
    context = getActivity().getApplicationContext(); 
    // make dialog object 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    // get the layout inflater 
    LayoutInflater li = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    // inflate our custom layout for the dialog to a View 
    View view = li.inflate(R.layout.activity_dark, null); 
    // inform the dialog it has a custom View 
    builder.setView(view); 
    // and if you need to call some method of the class 
    NumberPicker np = (NumberPicker) view 
      .findViewById(R.id.numberPicker); 

    // create the dialog from the builder then show 
    return builder.create(); 
} 

}

現在在我的片段,點擊按鈕後,我試圖表明我的號碼選擇器:

FragmentManager fm = getFragmentManager(); 
      NumberPickerCustomDialog yourDialog = new NumberPickerCustomDialog(); 
      yourDialog.show(fm, "some_optional_tag"); 

我的佈局看起來:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" 
android:gravity="center"> 

<net.simonvt.numberpicker.NumberPicker 
    android:id="@+id/numberPicker" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

我包括庫到項目,複製佈局和遇到錯誤:

04-17 11:33:28.264: E/AndroidRuntime(10891): FATAL EXCEPTION: main 
04-17 11:33:28.264: E/AndroidRuntime(10891): android.view.InflateException: Binary XML file line #8: Error inflating class net.simonvt.numberpicker.NumberPicker 
04-17 11:33:28.264: E/AndroidRuntime(10891): at android.view.LayoutInflater.createView(LayoutInflater.java:613) 
04-17 11:33:28.264: E/AndroidRuntime(10891): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687) 
04-17 11:33:28.264: E/AndroidRuntime(10891): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
04-17 11:33:28.264: E/AndroidRuntime(10891): at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
04-17 11:33:28.264: E/AndroidRuntime(10891): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
04-17 11:33:28.264: E/AndroidRuntime(10891): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
04-17 11:33:28.264: E/AndroidRuntime(10891): at pl.package.jp.Me.NumberPickerCustomDialog.onCreateDialog(NumberPickerCustomDialog.java:26) 
04-17 11:33:28.264: E/AndroidRuntime(10891): at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:295) 
04-17 11:33:28.264: E/AndroidRuntime(10891): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911) 
04-17 11:33:28.264: E/AndroidRuntime(10891): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) 
04-17 11:33:28.264: E/AndroidRuntime(10891): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
04-17 11:33:28.264: E/AndroidRuntime(10891): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) 
04-17 11:33:28.264: E/AndroidRuntime(10891): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429) 

我在做什麼錯?

+0

有一些東西錯您用於對話框的xml佈局。發佈此佈局xml文件R.layout.activity_dark。 – Triode

+1

net.simonvt.numberpicker.NumberPicker這個軟件包名稱可能是錯誤的。檢查您的自定義視圖的包裝名稱 – Triode

+0

如果您不介意,您可以發佈您的NumberPicker的代碼 – Triode

回答

6

好的,我解決了這個問題。該庫需要定義樣式,即:

<style name="SampleTheme" parent="android:Theme"> 
    <item name="numberPickerStyle">@style/NPWidget.Holo.NumberPicker</item> 
</style> 

<style name="SampleTheme.Light" parent="android:Theme.Light"> 
    <item name="numberPickerStyle">@style/NPWidget.Holo.Light.NumberPicker</item> 
</style> 

,並在AndroidManifest.xml中,風格必須爲活動的,要使用你的自定義數字選擇器來定義:

<activity android:name=".Activity" 
     android:theme="@style/SampleTheme.Light"> 

</activity> 
+0

我面臨同樣的問題,但如果我只想在DialogFragment中設置主題而不是活動,並且DialogFragment不是活動,所以我們不能通過執行您的解決方案在Menifest文件中提及它,整個活動將得到此主題,而不是我只想在對話任何解決方案 – ANinJa