11-06 05:58:03.239: ERROR/AndroidRuntime(778): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
package com.shen.sqlite;import java.util.ArrayList;import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;public class sqlite extends Activity {
/** Called when the activity is first created. */
public static final String bdName = "code.db";
public static final int Version = 1;
private Myhandle handle;
private SQLiteDatabase dataset;
private ArrayAdapter<String> adapt;
private Cursor cursor;
private ArrayList<String> infoList=new ArrayList<String>(); @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
handle = new Myhandle(this, bdName, null, Version);
dataset = handle.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Myhandle.COUNTRY, "china");
values.put(Myhandle.CODE, 86);
dataset.insert(Myhandle.TableName, Myhandle.ID, values);
dataset.insert(Myhandle.TableName, Myhandle.ID, null);
values.clear();
values.put(Myhandle.COUNTRY, "usa");
values.put(Myhandle.CODE, 88);
dataset.update(Myhandle.TableName, values, Myhandle.ID + "=2", null);
dataset.execSQL("INSERT INTO " + Myhandle.TableName + "("
+ Myhandle.COUNTRY + "," + Myhandle.CODE + ") VALUES"
+ "('JAP','89')"); values.put(Myhandle.COUNTRY, "china");
cursor = dataset.rawQuery("Select * from " + Myhandle.TableName, null);
// TextView text = new TextView(this);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
String a = cursor.getString(1);
String b = cursor.getString(2);
infoList.add(a + b);
//
}
ListView sp = (ListView) findViewById(R.id.spinner);
adapt = new ArrayAdapter<String>(this, R.layout.main, infoList);
sp.setAdapter(adapt);
} public void onDestroy() {
dataset.delete(Myhandle.TableName, null, null);
super.onDestroy();
}
}

解决方案 »

  1.   

    抛出的异常已经写的很明白呢:
    java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView 

    adapt = new ArrayAdapter <String>(this, R.layout.main, infoList);
    这句中布局main中的布局可能不是TextView对象。
     
    adapt = new ArrayAdapter <String>(A1, A2, A3);
    参数A2是一个“resource ID to be a TextView 
      

  2.   

    这是我main.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">
    <TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
    <ListView android:id="@+id/spinner" android:layout_width="fill_parent"
    android:layout_height="wrap_content"></ListView>
    </LinearLayout>
      

  3.   

    adapt = new ArrayAdapter <String>(this, R.layout.main, infoList);
    不正确吧
      

  4.   

    第2个参数应该是ListView中每个选择项的样式,可以使用系统自带的android.R.layout.xxx,也可以是自定义的,仅包含TextView。
      

  5.   

    你的main.xml要更改为:
    <?xml version="1.0" encoding="utf-8"?>
    <TextView  xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
    android:layout_height="wrap_content" />因为根节点必须是TextView,不然就会抛“ArrayAdapter requires the resource ID to be a TextView”
      

  6.   

    R.layout.main要使用系统提供的显示layout,或者你就自己定义一个显示list的layout.
      

  7.   

    R.layout.main只能是一个TextView的layout
      

  8.   

    你的adapter的资源文件的顶级结点必须是TextView