2013-04-11 30 views
6

Tôi đang cố gắng thêm một ImageView vào một phân đoạn tùy chọn để hiển thị bản xem trước của cài đặt màu. Tôi đang truy cập vào thể hiện của imageview thông qua phương thức onCreateView để đặt màu thử nghiệm và nó sẽ hiển thị. Tuy nhiên nó chỉ hoạt động nếu tôi không gọi addPreferencesFromResource trong phương thức onCreate - đó là vấn đề vì các tùy chọn phải được thêm vào. Ngoài ra nếu tôi rời khỏi cuộc gọi để thêmPreferencesFromResource, nhưng loại bỏ toàn bộ phương thức onCreateView chương trình sẽ chạy (albiet mà không có các imageview có thể cập nhật).Lỗi lạ khi sử dụng onCreateView trong PreferenceFragment khi gọi addPreferencesFromResource từ onCreate

Các lỗi trong cả hai trường hợp là "Nội dung có quan điểm với thuộc tính id 'android.R.id.list' đó không phải là một lớp ListView"

Tôi đã cố gắng để truy cập ImageView từ onCreate, nhưng sau đó các mục bố trí được thổi phồng và tôi dường như không thể truy cập vào cá thể thực tế được hiển thị.

Lỗi từ 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 

Ở đây PreferenceActivity với Fragment inline:

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; 
    } 


}} 

Dưới đây là định nghĩa ưu tiên trong 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> 

Dưới đây là cách bố trí có chứa các ImageView trong 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> 

Mặc dù lỗi tôi không sử dụng ListView hoặc ListFragment ở bất kỳ đâu trong dự án của mình. Điều này gần như có vẻ giống như một lỗi android. Bất kỳ đề nghị sẽ được đánh giá cao.

Trả lời

18

Khi bạn tạo một layot ​​tùy chỉnh cho một PreferenceActivity hoặc một PreferenceFragment bạn phải cung cấp một ListView với id android.R.id.list nơi Preferences đi đến.

<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

Ồ thật tuyệt vời. Cảm ơn bạn Doctoror Drive!Nó sẽ khá hữu ích nếu trang android thực sự đề cập đến điều này trong khi họ đang nói chuyện với bạn thông qua mã. –

+0

Điều này có được đề cập trong tài liệu ở mọi nơi không? – Alex

+0

@Alex không thể tìm thấy nó, nhưng ngoại lệ là khá tự giải thích. Ngoài ra, nó trở nên rõ ràng nếu bạn nhìn vào mã nguồn, vì 'PreferenceScreen' thổi phồng sang' ListView', và 'PreferenceFragment' tìm kiếm nó bằng' android.R.id.content'. –

1

Tôi cũng gặp sự cố này hôm nay. Nó bắt nguồn từ việc sử dụng Hướng dẫn Android ở đây: http://developer.android.com/guide/topics/ui/settings.html#Fragment

Về cơ bản, tôi thấy vấn đề đến từ việc sử dụng các tập tin preferences.xml:

addPreferencesFromResource(R.xml.preferences); 

Khi tôi nhận xét này ra, sai ra đi và hoạt động của tôi bắt đầu hiển thị, mặc dù không được phổ biến theo sở thích. Tôi sẽ cố gắng tìm kiếm và theo dõi ở đây.

Tôi hy vọng điều này đã giúp cho đến nay! Ngoại lệ ném không phải là rất hữu ích.

+0

Tại sao downvote ? Tôi đã có vấn đề chính xác này và đây là vấn đề đã gây ra nó. –

0

tôi đã cùng một vấn đề, và giải pháp là để đảm bảo rằng tôi đang nhập khẩu các thư viện đúng Fragment:

import android.app.Fragment; 

(không phải phiên bản từ thư viện hỗ trợ)

Các vấn đề liên quan