2014-04-11 12 views
9

tôi đã cố gắng hướng dẫn này http://windrealm.org/tutorials/android/android-listview.php "A Simple Android ListView Ví dụ"java.lang.IllegalStateException: ArrayAdapter đòi hỏi ID tài nguyên là một TextView

Nhưng trong thử nghiệm của tôi trong Eclipse Tôi đã sụp đổ trong ứng dụng Android.

Trong LogCat Tôi đã này:

04-11 20:17:24.170: E/AndroidRuntime(7062): 
java.lang.IllegalStateException: ArrayAdapter requires the resource ID 
to be a TextView 

Tại sao sự sụp đổ?

lớp java

private ListView mainListView; 
private ArrayAdapter<String> listAdapter; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mains); 

     mainListView = (ListView) findViewById(R.id.mainListView); 

     String[] xRemote = Remote.split(";"); 

     ArrayList<String> planetList = new ArrayList<String>(); 

     planetList.addAll(Arrays.asList(xRemote)); 

     listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, 
       planetList); 

     listAdapter.addAll(xRemote); 

     mainListView.setAdapter(listAdapter); 

mains.xml

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

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

     <ListView android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/mainListView"> 
     </ListView  
    </LinearLayout> 

simplerow.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/rowTextView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:textSize="16sp" > 
    </TextView> 

</LinearLayout> 

Trả lời

24

Sử dụng

listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, 
    R.id.rowTextView, planetList); 

Các documentation nói:

Theo mặc định lớp này hy vọng rằng, với điều kiện tài liệu tham khảo id nguồn một TextView duy nhất. Nếu bạn muốn sử dụng bố cục phức tạp hơn, hãy sử dụng các hàm tạo cũng có id trường. Id trường đó cần tham chiếu một TextView trong tài nguyên bố cục lớn hơn.

1

Trong Remo simplerow.xml ve LinearLayout và trực tiếp sử dụng TextView là gốc

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