我的数据库列表视图不起作用

时间:2013-08-21 07:08:17

标签: android sqlite

您好我在我的活动中创建了列表视图,其中我想要显示数据库中的内容。但是发生了一个错误,说没有找到列表视图任何人都可以给我一个更好的解决方案来克服错误吗?

我的日志猫位于下方

08-21 06:54:52.103: E/AndroidRuntime(6874): FATAL EXCEPTION: main
08-21 06:54:52.103: E/AndroidRuntime(6874): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.neochat/com.neochat.Friends}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.neochat/com.neochat.Friends_list}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
08-21 06:54:52.103: E/AndroidRuntime(6874):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-21 06:54:52.103: E/AndroidRuntime(6874):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-21 06:54:52.103: E/AndroidRuntime(6874):     at android.app.ActivityThread.access$600(ActivityThread.java:141)

我正在为下面的listview java类提供我的代码

public class Friends_list extends ListActivity implements OnItemClickListener{

    private ArrayList<String> results = new ArrayList<String>();

    private String tableName = LoginDataBaseAdapter.tableName;

    private SQLiteDatabase newDB;

    ListView listview;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        openAndQueryDatabase();

        displayResultList();

        setContentView(R.layout.activity_friends);
        context=this;

        listview=(ListView)findViewById(R.id.listView1);

        //String[] arrayColumns=new String[]{"Name,Username"};
        //int[]arrayViewIDs=new int[]{R.id.textViewSMSSender,R.id.textViewMessageBody};
        Cursor cursor;


        //cursor.getContentResolver().query(Uri.parse("content://sms/inbox"),null,null,null,null);

        //@SuppressWarnings("deprecation")
        //SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.friendsms, cursor, arrayColumns, arrayViewIDs);
        //listview.setAdapter(adapter);
        // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        // android.R.layout.simple_list_item_activated_1, android.R.id.text1,value);
        listview.setOnItemClickListener(this);
    }

    private void displayResultList() {

        TextView tView = new TextView(this);
        tView.setText("Friends");
    getListView().addHeaderView(tView);
    setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, results));
        getListView().setTextFilterEnabled(true);
    }

    private void openAndQueryDatabase() {
        try {
            LoginDataBaseAdapter loginDataBaseAdapter = new LoginDataBaseAdapter(this.getApplicationContext());
            newDB=loginDataBaseAdapter.getWritableDatabase();
            Cursor c=newDB.rawQuery("  SELECT    NAME   ,   USERNAME     FROM    "+ tableName, null);
            if (c != null ) { 
                if  (c.moveToFirst()) {
                    do {
                        String Name=c.getString(c.getColumnIndex("NAME"));
                        String Username=c.getString(c.getColumnIndex("USERNAME"));
                    } while(c.moveToNext());                
                }
            }
        } catch (SQLiteException se) {
            Log.e(getClass().getSimpleName(), "Could not open the database");
        } finally {
            if(newDB!=null)
                newDB.execSQL("   DELETE    FROM   "+ tableName);
            newDB.close();
        }
    }

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Intent in2=new Intent(Friends_list.this,InboxActivity.class);
        startActivity(in2);
    }       
}

以下是我创建数据库的代码

public class LoginDataBaseAdapter extends SQLiteOpenHelper {

    public static final int NAME_COLUMN=2;
    static final String DATABASE_NAME = "UserDetails.db";
    static final int DATABASE_VERSION = 1;
    static final String tableName="Signup";
    static final String DATABASE_CREATE = "create table "   
   + " Signup "+ " " + "  "
   + " ( "    
   + " ID "  
   + " integer primary key autoincrement , "   
   + " NAME text , USERNAME text , PASSWORD text , EMPLOYEE_CODE text," 
   + " MOBILE_NUMBER integer ); ";

    public SQLiteDatabase db;
    private final Context context;
    private DataBaseHelper dbHelper;

这是我的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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingTop="6dp"
    tools:context=".Friends" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </TabHost>
    <ListView
        android:id="@+id/listView1"
        style="@style/WhiteText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="43dp">
    </ListView>   
</LinearLayout>

5 个答案:

答案 0 :(得分:2)

这是因为您在setContentView之前调用了displayResultList()函数,毕竟在找到ListView的id之前....

所以改为:

 @Override
  protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_friends);


   listview=(ListView)findViewById(R.id.listView1);
    openAndQueryDatabase();
    displayResultList();}

答案 1 :(得分:2)

在您的代码中,您在setContentView方法中调用了onCreate。您的R.layout.activity_friends文件包含列表视图,但其ID为listView1

如果您仔细查看异常,那么您将看到android期待一个id为list的列表视图。

您可以执行以下操作之一来解决异常

  1. 将xml中listView的ID从listView1更改为list

  2. 由于您的活动从ListActivity扩展,因此它继承了一个ID为android.R.id.list的ListView。在这种情况下,只需注释对setContentView()方法的调用。

  3. 从活动而不是ListActivity扩展。
  4. 请阅读ListActivity类http://developer.android.com/reference/android/app/ListActivity.html

    的Android文档

答案 2 :(得分:0)

不要扩展列表活动只是这个解决的活动

答案 3 :(得分:0)

因为你的活动扩展了ListActivity,ListActivity包含一个ListView,你可以通过Code获取ListView.mListView = getListView();,你不需要setContentView()

答案 4 :(得分:0)

1,你应该删除“setContentView(R.layout.activity_friends);” 2.mothed“findViewById()”应该由mothed getListView();

加入