내 응용 프로그램에서, 나는 SharedPreference 변수에 사용자가 저장 한 값을 기반으로 배열을 선언해야합니다. 문제는 내 클래스에서 onCreate()를 호출하기 전에 배열의 크기를 선언해야하므로 정적 블록에서 배열을 선언해야한다는 것입니다.클래스의 정적 블록에서 안드로이드 getSharedPreferences
부모 배열이있는 내 액티비티에 ExpandableList가 다음 7 일간의 날짜로 있습니다. 내가 정적 블록 내부의 배열을 선언하지 않으면
static int plannerSlotCount=7;
static public String[] parent = new String[plannerSlotCount];
static
{
Calendar cal = Calendar.getInstance();
String strdate = null;
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
int i;
for(i=0;i<plannerSlotCount;i++)
{
if (cal != null) {
strdate = sdf.format(cal.getTime());
}
parent[i] = strdate;
cal.add(Calendar.HOUR_OF_DAY,24);
}
}
은, 그때 그래서, 정적 블록 자체 I에서 배열의 내용을 선언해야
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
에 오류가 추측.
내가 표시 할 일 수를 변경하고 싶습니다 (현재 7로 설정되어 있습니다). 그래서 SharedPreference 변수에 숫자를 저장하고 배열에 초기화하려고 생각했습니다.
내가 직면하고 문제는
SharedPreferences preferences = getSharedPreferences(Settings.PREF_SETTINGS_FILE_NAME, MODE_PRIVATE);
final int slotCounter = preferences.getInt("slotCount", 7);
나에게
Cannot make a static reference to the non-static method getSharedPreferences(String, int) from the type ContextWrapper
이를 달성하기 위해 가능한 모든 방법이 있나요 없다는 오류를 준다이다?
내 질문을 편집했습니다. 가능한 경우 내용을 확인하고 제안하십시오. – Swayam
어디에서 sharedPreference를 검색하려고합니까? – Blackbelt
정적 블록 안에서 처리하고 싶습니다. 그러나 오류가 발생합니다. 내 질문에 주어진 코드가 작동하지만 SharedPreferences 너무 포함 할 싶습니다. – Swayam