2012-04-15 83 views
0

我擴展了DialogPreference類並希望我可以使解決方案像this一樣工作。擴展DialogPreference時Android類拋出異常

我想實現我的系統我TimePreference類,但調用PreferenceActivity當我得到一個運行時異常

04-15 16:47:31.555: W/MediaPlayer(28689): mediaplayer went away with unhandled events 
04-15 16:47:31.555: W/MediaPlayer(28689): mediaplayer went away with unhandled events 
04-15 16:47:31.594: D/libEGL(28689): loaded /system/lib/egl/libGLES_android.so 
04-15 16:47:31.609: D/libEGL(28689): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so 
04-15 16:47:31.633: D/libEGL(28689): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so 
04-15 16:47:31.637: D/libEGL(28689): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so 
04-15 16:47:31.793: D/OpenGLRenderer(28689): Enabling debug mode 0 
04-15 16:47:36.938: D/AndroidRuntime(28689): Shutting down VM 
04-15 16:47:36.938: W/dalvikvm(28689): threadid=1: thread exiting with uncaught exception (group=0x40a4c1f8) 
04-15 16:47:37.024: E/AndroidRuntime(28689): FATAL EXCEPTION: main 
04-15 16:47:37.024: E/AndroidRuntime(28689): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.timer/app.timer.PrefsActivity}: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.os.Looper.loop(Looper.java:137) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.app.ActivityThread.main(ActivityThread.java:4424) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at java.lang.reflect.Method.invokeNative(Native Method) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at java.lang.reflect.Method.invoke(Method.java:511) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at dalvik.system.NativeStart.main(Native Method) 
04-15 16:47:37.024: E/AndroidRuntime(28689): Caused by: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:205) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.preference.Preference.getPersistedString(Preference.java:1396) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at app.timer.TimePreference.onSetInitialValue(TimePreference.java:110) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.preference.Preference.dispatchSetInitialValue(Preference.java:1312) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.preference.Preference.onAttachedToHierarchy(Preference.java:1124) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.preference.PreferenceGroup.addPreference(PreferenceGroup.java:156) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:97) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:38) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.preference.GenericInflater.rInflate(GenericInflater.java:488) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.preference.GenericInflater.rInflate(GenericInflater.java:493) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.preference.GenericInflater.inflate(GenericInflater.java:326) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.preference.GenericInflater.inflate(GenericInflater.java:263) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:269) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1403) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at app.timer.PrefsActivity.onCreate(PrefsActivity.java:12) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.app.Activity.performCreate(Activity.java:4465) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
04-15 16:47:37.024: E/AndroidRuntime(28689): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
04-15 16:47:37.024: E/AndroidRuntime(28689): ... 11 more 
04-15 16:47:37.137: D/dalvikvm(28689): GC_CONCURRENT freed 230K, 4% free 9362K/9671K, paused 2ms+13ms 

我pref.xml看起來是這樣的,如果我刪除我的TimePreference工作

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 
    <PreferenceCategory 
     android:key="time_settings" 
     android:title="@string/time_settings" 
     android:summary="@string/time_settings_summary"> 
     <app.timer.TimePreference 
      android:key="synchronize" 
      android:title="@string/synchronize" 
      android:summary="@string/synchronize_summary" 
      android:icon="@drawable/ic_dialog_time" /> 
    </PreferenceCategory> 
    <PreferenceCategory 
     android:key="sound_cat" 
     android:title="@string/sound_cat" 
     android:summary="@string/sound_cat_summary"> 
     <CheckBoxPreference 
      android:key="mute" 
      android:title="@string/mute" 
      android:summary="@string/mute_summary" 
      android:icon="@drawable/ic_audio_vol" /> 
     <ListPreference 
       android:key="beep_interval" 
       android:title="@string/beep_interval" 
       android:summary="@string/beep_interval_summary" 
       android:entries="@array/beep_interval_options" 
       android:entryValues="@array/beep_interval_option_values" 
       android:defaultValue="2" 
       android:icon="@drawable/ic_audio_alarm" />  
    </PreferenceCategory> 
    <PreferenceCategory 
     android:key="screen" 
     android:title="@string/screen_cat" 
     android:summary="@string/screen_cat_summary"> 
     <CheckBoxPreference 
      android:key="screen_dim" 
      android:title="@string/screen_dim" 
      android:summary="@string/screen_dim_summary" 
      android:icon="@drawable/ic_menu_view" /> 
     <ListPreference 
       android:key="format" 
       android:title="@string/format" 
       android:summary="@string/format_summary" 
       android:entries="@array/format_options" 
       android:entryValues="@array/format_option_values" 
       android:defaultValue="0" 
       android:icon="@drawable/ic_menu_edit" /> 
     <CheckBoxPreference 
      android:key="indigo" 
      android:title="@string/indigo" 
      android:summary="@string/indigo_summary" 
      android:icon="@drawable/brightness" /> 
    </PreferenceCategory> 
    <PreferenceCategory 
     android:title="@string/about" 
     android:summary="@string/about_summary" >   
    <ListPreference 
     android:key="version_build" 
     android:enabled="false" 
     android:title="version 1.01" 
     android:summary="build 3" /> 
    </PreferenceCategory> 
</PreferenceScreen> 

這個我的TimePreference類

package app.timer; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.preference.DialogPreference; 
import android.text.format.DateFormat; 
import android.util.AttributeSet; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 
import android.widget.TimePicker; 

public class TimePreference extends DialogPreference { 
    protected int lastHour=0; 
    protected int lastMinute=0; 
    protected boolean is24HourFormat; 
    protected TimePicker picker=null; 
    protected TextView timeDisplay; 

    public TimePreference(Context ctxt) { 
     this(ctxt, null); 
    } 

    public TimePreference(Context ctxt, AttributeSet attrs) { 
     this(ctxt, attrs, 0); 
    } 

    public TimePreference(Context ctxt, AttributeSet attrs, int defStyle) { 
     super(ctxt, attrs, defStyle); 

     is24HourFormat = DateFormat.is24HourFormat(ctxt); 
     setPositiveButtonText("Set"); 
     setNegativeButtonText("Cancel"); 
    } 

    @Override 
    public String toString() { 
     if(is24HourFormat) { 
      return ((lastHour < 10) ? "0" : "") 
        + Integer.toString(lastHour) 
        + ":" + ((lastMinute < 10) ? "0" : "") 
        + Integer.toString(lastMinute); 
     } else { 
      int myHour = lastHour % 12; 
      return ((myHour == 0) ? "12" : ((myHour < 10) ? "0" : "") + Integer.toString(myHour)) 
        + ":" + ((lastMinute < 10) ? "0" : "") 
        + Integer.toString(lastMinute) 
        + ((lastHour >= 12) ? " PM" : " AM"); 
     } 
    } 

    @Override 
    protected View onCreateDialogView() { 
     picker=new TimePicker(getContext().getApplicationContext()); 
     return(picker); 
    } 

    @Override 
    protected void onBindDialogView(View v) { 
     super.onBindDialogView(v); 
     picker.setIs24HourView(is24HourFormat); 
     picker.setCurrentHour(lastHour); 
     picker.setCurrentMinute(lastMinute); 
    } 

    @Override 
    public void onBindView(View view) { 
     View widgetLayout; 
     int childCounter = 0; 
     do { 
      widgetLayout = ((ViewGroup) view).getChildAt(childCounter); 
      childCounter++; 
     } while (widgetLayout.getId() != android.R.id.widget_frame); 
     ((ViewGroup) widgetLayout).removeAllViews(); 
     timeDisplay = new TextView(widgetLayout.getContext()); 
     timeDisplay.setText(toString()); 
     ((ViewGroup) widgetLayout).addView(timeDisplay); 
     super.onBindView(view); 
    } 

    @Override 
    protected void onDialogClosed(boolean positiveResult) { 
     super.onDialogClosed(positiveResult); 

     if (positiveResult) { 
      picker.clearFocus(); 
      lastHour=picker.getCurrentHour(); 
      lastMinute=picker.getCurrentMinute(); 

      String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute); 

      if (callChangeListener(time)) { 
       persistString(time); 
       timeDisplay.setText(toString()); 
      } 
     } 
    } 

    @Override 
    protected Object onGetDefaultValue(TypedArray a, int index) { 
     return(a.getString(index)); 
    } 

    @Override 
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { 
     String time=null; 

     if (restoreValue) { 
      if (defaultValue==null) { 
       time=getPersistedString("00:00"); 
      } 
      else { 
       time=getPersistedString(defaultValue.toString()); 
      } 
     } 
     else { 
      if (defaultValue==null) { 
       time="00:00"; 
      } 
      else { 
       time=defaultValue.toString(); 
      } 
      if (shouldPersist()) { 
       persistString(time); 
      } 
     } 

     String[] timeParts=time.split(":"); 
     lastHour=Integer.parseInt(timeParts[0]); 
     lastMinute=Integer.parseInt(timeParts[1]);; 
    } 
} 

回答

1

java.lang.ClassCastException:java.lang.Boolean不能轉換爲java.lang.String

上面的代碼表示在你的代碼中的某些地方你試圖將一個布爾變量轉換爲字符串。只需檢查一次

+0

錯誤應該在TimePreference.java:110 – 2012-04-15 15:24:08