2012-06-05 2 views
0

나는 내 자신의 Preference 클래스를 작성하는 데 어려움을 겪고 있습니다. 환경 설정에 데이터를 저장하는 방법은 Preference 클래스의 "지속"그룹의 메소드를 사용하는 것 같습니다. 그러나, 내 취향에, 나는 색상 선택기 대화 상자를 열고 대화 상자 내에서 환경 설정을 저장해야합니다 colorChanged 재정의. 내가 트리거 onPreferenceChangeListener을 강제로 callChangeListener를 사용했지만, 그것은과 충돌 :Preferences 클래스의 내부 클래스에서 "persistInt"를 호출 할 수 없습니다.

06-05 10:21:46.396: ERROR/AndroidRuntime(516): FATAL EXCEPTION: main 
java.lang.IllegalAccessError: tried to access method android.preference.Preference.persistInt:(IIII)V from class android.preference.ColorSelectionPreference$1 
at android.preference.ColorSelectionPreference$1.colorChanged(ColorSelectionPreference.java:55) 
at android.apis.graphics.ColorPickerDialog.onClick(ColorPickerDialog.java:168) 

(6/5/12 12시 20분 UPDATE를 :) 내가 응용 프로그램을 실행하고 색 기본 설정을 변경하려고 할 때마다, 내가 얻을 동일한 오류. callChangeListener하지 않고, 환경 설정 데이터는 (아마도) 저장하지만 onPreferenceChangeListener가 트리거되지 않습니다

06-05 12:20:23.691: ERROR/AndroidRuntime(2834): FATAL EXCEPTION: main 
java.lang.IllegalAccessError: tried to access method android.preference.ColorSelectionPreference.callChangeListener:(IIII)V from class android.preference.ColorSelectionPreference$1 
at android.preference.ColorSelectionPreference$1.colorChanged(ColorSelectionPreference.java:52) 
at android.apis.graphics.ColorPickerDialog.onClick(ColorPickerDialog.java:168) 

여기에 실제 클래스의 :

package android.preference; 

import android.apis.graphics.ColorPickerDialog; 
import android.content.Context; 
import android.content.SharedPreferences; 
import android.graphics.Color; 
import android.util.AttributeSet; 

public class ColorSelectionPreference extends Preference { 
    private Context mContext; 
    private int mColor; 

    public ColorSelectionPreference(Context context) { 
     super(context); 
     mContext = context; 
    } 

    public ColorSelectionPreference(Context context, AttributeSet attr) { 
     super(context, attr); 
     mContext = context; 
    } 

    public int getColor() { 
     return mColor; 
    } 

    public void setColor(int color) { 
     mColor = color; 
    } 

    @Override 
    public void onClick() { 
     //get original preference 
     //set ColorPickerDialog to original preference color or default color 
     ColorPickerDialog dialog = new ColorPickerDialog(mContext, new ColorPickerDialog.OnColorChangedListener() { 
      public void colorChanged(int a, int r, int g, int b) { 
       int selectedColor = Color.argb(a,r,g,b); 
       setColor(selectedColor); 

       /*** crashes on callChangeListener ***/ 
       //SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); 
       //SharedPreferences.Editor edit = prefs.edit(); 
       //edit.putInt(getKey(), selectedColor); 
       //edit.commit(); 
       //callChangeListener(selectedColor); 

       /*** the offending code, error refers to this line ***/ 
       persistInt(selectedColor); 

       /*** tried this as well by request on IRC ***/ 
       //ColorSelectionPreference.this.persistInt(selectedColor); 
      } 
     }, mColor); 
     dialog.show(); 
    } 
} 
+0

'ColorSelectionPreference.this.persistInt (selectedColor);'를 시도했다. 나는'ColorSelectionPreference.persistInt (selectedColor); 시도하는 것이 좋습니다. 이것은 내가 전에 가지고 있었던 문제처럼 보인다. 내가 이클립스에서로드하지 않고 확신 할 수 없다. – cstrutton

+0

persistInt는 정적 메서드가 아닙니다. – moonlightcheese

+0

지금은'Handler'를'Preference' 클래스로 설정하고 변경 사항이 등록 될 때'PreferenceActivity'를 다시 호출하여이 문제를 해결해야합니다. 그러나, 이건 정말 해키하고 누군가가 방법을 알고 있다면 나는 오히려 그것을 올바른 방법으로 할 것입니다. – moonlightcheese

답변

0

이에 처리기를 사용하는 해키 해결 방법입니다 내부 클래스의 메인 클래스를 다시 콜합니다. 그것은 예쁘지 않지만 작동합니다.

package android.preference; 

import android.apis.graphics.ColorPickerDialog; 
import android.content.Context; 
import android.content.SharedPreferences; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.util.AttributeSet; 

public class ColorSelectionPreference extends Preference { 
    private Context mContext; 
    private int mColor; 

    private final Handler mHandler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
      if(msg.getData().containsKey("color")) { 
       int color = msg.getData().getInt("color"); 
       setColor(color); 
      } 
     } 
    }; 

    public ColorSelectionPreference(Context context) { 
     super(context); 
     mContext = context; 
    } 

    public ColorSelectionPreference(Context context, AttributeSet attr) { 
     super(context, attr); 
     mContext = context; 
    } 

    public int getColor() { 
     return mColor; 
    } 

    public void setColor(int color) { 
     mColor = color; 
     persistInt(new Integer(color)); 
    } 

    @Override 
    public void onClick() { 
     //get original preference 
     //set ColorPickerDialog to original preference color or default color 
     ColorPickerDialog dialog = new ColorPickerDialog(mContext, new ColorPickerDialog.OnColorChangedListener() { 
      public void colorChanged(int a, int r, int g, int b) { 
       int selectedColor = Color.argb(a,r,g,b); 
       Bundle bundle = new Bundle(); 
       bundle.putInt("color", selectedColor); 
       Message msg = new Message(); 
       msg.setData(bundle); 
       mHandler.sendMessage(msg); 

       //setColor(selectedColor); 

       /*** tried this, but the onPreferenceChangedListener never gets triggered, so this won't work ***/ 
       //SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); 
       //SharedPreferences.Editor edit = prefs.edit(); 
       //edit.putInt(getKey(), selectedColor); 
       //edit.commit(); 
       //callChangeListener(selectedColor); 

       /*** the offending code, error refers to this line ***/ 
       //container. 

       /*** tried this as well by request on IRC ***/ 
       //ColorSelectionPreference.this.persistInt(selectedColor); 
      } 
     }, mColor); 
     dialog.show(); 
    } 
} 
0

방금이 문제가 발생했습니다. 나는 그것을 이해하는 척하지 않는다. 그러나 이것은 쉬운 수정처럼 보일 것이다.

// ...why is this necessary? what is special about Preference.persistInt? 
@Override protected boolean persistInt(int value) 
{ 
    return super.persistInt(value); 
} 
관련 문제