2013-04-11 11 views
6

Próbuję dodać ImageView do fragmentu preferencji, aby pokazać podgląd ustawienia kolorów. Uzyskuję dostęp do instancji widoku obrazu za pomocą metody onCreateView, ustawiając kolor testowy i zostanie wyświetlony. Działa to jednak tylko wtedy, gdy nie wzywam metody addPreferencesFromResource w metodzie onCreate, co jest problemem, ponieważ preferencje muszą zostać dodane. Również, jeśli zostawiam wywołanie metody addPreferencesFromResource, ale usuniemy całą metodę onCreateView, uruchomi się program (albiet bez możliwej do uaktualnienia odsłony).Dziwny błąd przy użyciu onCreateView w PreferenceFragment podczas wywoływania addPreferencesFromResource z onCreate

Błąd w obu przypadkach jest „Content ma widok z atrybutem id«android.R.id.list», który nie jest klasą ListView”

Próbowałem otworzyć ImageView z onCreate, ale wtedy elementy układu są zawyżone i nie mogę uzyskać dostępu do rzeczywistej instancji, która jest wyświetlana.

Błąd z LogCat:

04-11 00:42:43.619: E/AndroidRuntime(5362): FATAL EXCEPTION: main 
04-11 00:42:43.619: E/AndroidRuntime(5362): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.signalwidget/com.example.android.signalwidget.SignalWidgetConfigure}: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class 

Tutaj PreferenceActivity z inline Fragment:

public class SigConfigure extends PreferenceActivity { 

private static int prefs=R.xml.pref_widget_colors; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //getFragmentManager().beginTransaction().replace(android.R.id.content, new ColorsFragment()).commit(); 

} 

@Override 
public void onBuildHeaders(List<Header> target) { 
    loadHeadersFromResource(R.xml.pref_headers, target); 
} 

public static class ColorsFragment extends PreferenceFragment { 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     addPreferencesFromResource(SignalWidgetConfigure.prefs); 


    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     super.onCreateView(inflater, container, savedInstanceState); 

     //just testing to see if the imageview can be accessed. 
     View v = inflater.inflate(R.layout.layout_pref_row, container, false); 
     ImageView iv = (ImageView) v.findViewById(R.id.color_preview); 
     iv.setBackgroundColor(Color.CYAN); 

     return v; 
    } 


}} 

Oto definicja preferencji w pref_widget_colors

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 

    <Preference 
     android:key="wifi_signal_color" 
     android:title="WiFi Signal Color" > 
     <intent 
      android:action="android.intent.action.VIEW" 
      android:targetClass="com.example.android.signalwidget.colorpicker.PickerActivity" 
      android:targetPackage="com.example.android.signalwidget" /> 
    </Preference> 
    <Preference 
     android:key="cell_signal_color" 
     android:title="Cell Signal Color" > 
     <intent 
      android:action="android.intent.action.VIEW" 
      android:targetClass="com.example.android.signalwidget.colorpicker.PickerActivity" 
      android:targetPackage="com.example.android.signalwidget" /> 
    </Preference> 

</PreferenceScreen> 

Oto układ zawierający ImageView w layout_pref_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/color_preview" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_marginTop="5dp" 
     android:background="#aaa" /> 
</LinearLayout> 

Mimo błędu nie używam ListView ani ListFragment w żadnym miejscu mojego projektu. Wygląda to prawie jak błąd Androida. Wszelkie sugestie będą mile widziane.

Odpowiedz

18

Podczas tworzenia niestandardowego layot ​​dla PreferenceActivity lub PreferenceFragment ty koniecznością dostarczyć ListView z id android.R.id.list gdzie Preferencje udać.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/color_preview" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_marginTop="5dp" 
     android:background="#aaaaaa" /> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="5dp" /> 

</LinearLayout> 
+1

Och, to jest niesamowite. Dziękuję Doktorze Drive!Byłoby bardzo pomocne, gdyby strona z Androidem wspomniała o tym, kiedy rozmawialiśmy przez kod. –

+0

Czy jest to wspomniane w dokumentach w dowolnym miejscu? – Alex

+0

@Alex nie mógł go znaleźć, ale wyjątek jest dość oczywisty. Ponadto staje się oczywiste, jeśli spojrzysz na kod źródłowy, ponieważ 'PreferenceScreen' nadyma się do' ListView', a 'PreferenceFragment' szuka go przez' android.R.id.content'. –

1

Miałem ten problem również dzisiaj. Wynikało to z wykorzystaniem Poradniki Android tutaj: http://developer.android.com/guide/topics/ui/settings.html#Fragment

Zasadniczo uważam, że problem pojawił się przy użyciu pliku preferences.xml:

addPreferencesFromResource(R.xml.preferences); 

Raz skomentował to uwagę, błąd odszedł i moja aktywność zaczęło się wyświetlać, choć nieopełnione zgodnie z preferencjami. Spróbuję to upolować i pójść tutaj.

Mam nadzieję, że to pomogło do tej pory! Zgłoszony wyjątek nie jest bardzo pomocny.

+0

dlaczego downvote ? Miałem dokładnie ten problem i to był problem, który go powodował. –

0

miałem ten sam problem, a rozwiązanie było zapewnienie, że importuje poprawną bibliotekę fragment:

import android.app.Fragment; 

(nie w wersji z biblioteki wsparcia)

Powiązane problemy