2012-10-06 65 views
3

我有一個成功使用ToggleButton的應用程序。我將該應用程序轉換爲JELLY BEAN(4.1.1)使用。 4.1.1有Switch小部件,這是一個更好看的ToggleButton小部件。這兩個部件都來自CompoundButtonAndroid CompoundButton Switch jumpDrawablesToCurrentState null crash

Android的對比文件是在這裏:

http://developer.android.com/guide/topics/ui/controls/togglebutton.html

它說:

切換按鈕和開關控制是CompoundButton的子類和功能相同的方式,這樣就可以實現自己的行爲一樣的方法。

所以我所做的就是採取含ToggleButton是我的活動佈局文件,將其複製到該目錄res/layout-v14/,取而代之的ToggleButton所有實例Switch。這意味着Android版本14和更高版本將使用Switch的佈局文件,下面的14將使用ToggleButton的佈局文件。除了部件名稱以外,每個XML都是相同的。

<Switch 
android:id="@+id/settings_some_option_on_off" 
android:textOn="@string/settings_toggle_on" 
android:textOff="@string/settings_toggle_off" 
android:gravity="center" 
android:paddingRight="@dimen/size_padding_minor" 
android:layout_weight="1" 
android:layout_gravity="center" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"/> 

在我的.java代碼中,我只使用了CompoundButton。根本不使用ToggleButtonSwitch

private CompoundButton mViewSomeOptionOnOff; 
... 
mViewSomeOptionOnOff = (CompoundButton) findViewById(R.id.settings_some_option_on_off); 
etc. 

當我在< 14上運行時,它工作的很好。和之前一樣。我得到了ToggeButton。當我在14上運行時,Android小部件框架中出現空崩潰。

我下載了Android源代碼。從碰撞回溯中,我確切知道撞車的位置。 Switch.java:

808  @Override 
809  public void jumpDrawablesToCurrentState() { 
810   super.jumpDrawablesToCurrentState(); 
811   mThumbDrawable.jumpToCurrentState(); <------ boom 
812   mTrackDrawable.jumpToCurrentState(); 
813  } 

thumb是一個新的屬性Switch。即便如此,我也不必定義它,對吧?它沒有被列爲強制性財產。

就像測試一樣,回到我的-v14佈局中,我將android:thumb設置爲drawable。然後我在第812行,track上找到了空值。我將android:track設置爲drawable,並且崩潰消失了。 那麼發生了什麼事?

爲什麼我碰到零碰撞?

我需要找到默認的TrackThumb可繪製表,並將它們複製到我的應用程序?

是我想要做的 - 使用ToggleButton和Switch - 不可能?

+0

看看你的logcat回溯錯誤。它必須引用調用Switch.java及其811行的代碼部分。我想這是在調用你的'findViewById(R.id.settings_some_option_on_off)'時。那是對的嗎? – Kyriog

+0

回溯是所有Android的東西。我的應用程序內沒有框架。崩潰發生在onCreate(),onStart()和onResume()成功完成之後。 – Syrinx

+0

我應該添加,崩潰發生在活動變得可見/可交互之前。 – Syrinx

回答

4

確保您的minSdkVersion是14 AndroidManifest.xml中

0

我有這個錯誤也和由API 14及以上設置主題解決了這個問題 - 因爲nandeesh評論。