3

다음은 this tutorial입니다. PreferenceActivity는 설정을 만드는 데 사용됩니다. 그러나 내가 좋아하면 Fragments를 사용할 수 있다고 언급되었으므로 나는 정상적인 활동에서 PreferenceFrament를 사용했다. 나는 응용 프로그램을 실행할 때, 그것은 다음과 같은 로그를 제공 추락 - 나는 SO에 대한 검색하면 PreferenceFragment의 RunTimeException

07-25 10:41:02.048 7515-7515/com.example.sunshine E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sunshine/com.example.sunshine.SettingsActivity}: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2063) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088) 
      at android.app.ActivityThread.access$600(ActivityThread.java:134) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199) 
      at android.os.Handler.dispatchMessage(Handler.java:99) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:4744) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class 
      at android.preference.PreferenceFragment.ensureList(PreferenceFragment.java:350) 
      at android.preference.PreferenceFragment.getListView(PreferenceFragment.java:336) 
      at android.preference.PreferenceFragment.bindPreferences(PreferenceFragment.java:330) 
      at android.preference.PreferenceFragment.onActivityCreated(PreferenceFragment.java:171) 
      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:852) 
      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1039) 
      at android.app.BackStackRecord.run(BackStackRecord.java:635) 
      at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1411) 
      at android.app.Activity.performStart(Activity.java:5017) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2036) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088) 
            at android.app.ActivityThread.access$600(ActivityThread.java:134) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:4744) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
            at dalvik.system.NativeStart.main(Native Method) 

, 나는 충돌이 다소 ListFragment 관련이 알게되었습니다. 하지만 난 내 애플 리케이션에서 그것을 사용하지 않았 확신합니다. PreferenceFragment가 호출했을 수도 있습니다. 나는 여기서 문제를 정확히 지적 할 수 없다. 그것을 해결하도록 도와 줄 수 있습니까? 내가 대신 조각을 위해 작동하도록 변경해야 다른 있나요

public class SettingsActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_settings); 
     if (savedInstanceState == null) { 
      getFragmentManager().beginTransaction() 
        .add(R.id.container, new SettingsFragment()) 
        .commit(); 
     } 
    } 

    public static class SettingsFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener { 

     public SettingsFragment() { 
     } 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      // Add 'general' preferences, defined in the XML file 
      addPreferencesFromResource(R.xml.pref_general); 

      // For all preferences, attach an OnPreferenceChangeListener so the UI summary can be 
      // updated when the preference changes. 
      bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key))); 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_settings, container, false); 
      return rootView; 
     } 

     /** 
     * Attaches a listener so the summary is always updated with the preference value. 
     * Also fires the listener once, to initialize the summary (so it shows up before the value 
     * is changed.) 
     */ 
     private void bindPreferenceSummaryToValue(Preference preference) { 
      // Set the listener to watch for value changes. 
      preference.setOnPreferenceChangeListener(this); 

      // Trigger the listener immediately with the preference's 
      // current value. 
      onPreferenceChange(preference, 
        PreferenceManager 
          .getDefaultSharedPreferences(preference.getContext()) 
          .getString(preference.getKey(), "")); 
     } 

     @Override 
     public boolean onPreferenceChange(Preference preference, Object value) { 
      String stringValue = value.toString(); 

      if (preference instanceof ListPreference) { 
       // For list preferences, look up the correct display value in 
       // the preference's 'entries' list (since they have separate labels/values). 
       ListPreference listPreference = (ListPreference) preference; 
       int prefIndex = listPreference.findIndexOfValue(stringValue); 
       if (prefIndex >= 0) { 
        preference.setSummary(listPreference.getEntries()[prefIndex]); 
       } 
      } else { 
       // For other preferences, set the summary to the value's simple string representation. 
       preference.setSummary(stringValue); 
      } 
      return true; 
     } 
    } 
} 

- 여기

는 우선 내가 사용하고 XML입니다 -

<?xml version="1.0" encoding="utf-8"?> 

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 
    <EditTextPreference 
     android:title="@string/pref_location_label" 
     android:key="@string/pref_location_key" 
     android:defaultValue="@string/pref_location_default" 
     android:inputType="text" 
     android:singleLine="true" 
     /> 
</PreferenceScreen> 

그리고 여기에 조각과 함께 SettingsActivity입니다 활동?

답변

3

마지막으로 발견했습니다. 나는 단지 android:id="@id/android:list" 속성을 가진 ListView를 내 프래그먼트의 XML에 넣어야했다. 내가 왜 그것을 필요로하는지 모르겠지만, 시행 착오로 발견했습니다.

+3

답변을 naveen ravindran이 (가) 게시했습니다. 누군가가 WORKS를 추가하는 이유에 대해 궁금해하는 경우에는 (소스 코드) 환경 설정 조각 -> bindpreferences()가 실제로 현재보기에서 android.r.id.list를 사용하여 목록보기를 찾으려고 시도하기 때문입니다. 결과보기가 목록보기가 아닌 경우 위의 예외가 발생합니다.이 목록은 환경 설정을 표시하는 데 반드시 필요합니다. " (그리고 네,이 또한 나를 위해 일한 것을 확인할 수 있습니다.) – Robbie

+0

^이것으로 마침내 해결되었습니다. – Ken

1

다음 ID로보기 만들기, 당신은 (아마도)입니다 말한대로 문제가 activity_settings.xml에 보인다 : android.R.id.list을 그리고 그것은 ListViewactivity_settings.xml와 아마의 problema을 발견 할 것이다, 그렇지 않으면 게시물을 확인하지 않습니다 XML이 잘못되었는지 확인하십시오.

+0

고마워요! 비록 내가 그것을 이미 풀었지만 당신의 대답은 내가 발견 한 것에 가깝다. – noob

+0

도와 줘서 다행 :)! – zozelfelfo

2

멍청한 의해 솔루션 이외에 추가 정보. 누군가가 WORKS를 추가하는 이유에 대해 궁금해하는 경우에는 (소스 코드) 환경 설정 조각 -> bindpreferences()가 실제로 현재보기에서 android.r.id.list를 사용하여 목록보기를 찾으려고 시도하고 결과 뷰가 listview가 아니면 위의 예외가 throw됩니다. 이 목록은 환경 설정을 표시하는 데 필수적입니다.

+0

설명 주셔서 감사합니다! – noob