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

时间:2013-08-23 07:29:23

标签: android listview

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

我的logcat在下面给出

    08-23 07:04:28.025: E/AndroidRuntime(24675): FATAL EXCEPTION: main    
    08-23 07:04:28.025: E/AndroidRuntime(24675): 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-23 07:04:28.025: E/AndroidRuntime(24675):    at     
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)    
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at     
    android.app.ActivityThread.startActivityNow(ActivityThread.java:2023)    
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at     
    android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)    
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at     
    android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)    
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at     
    android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749    
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at     
    android.widget.TabHost.setCurrentTab(TabHost.java:413)    
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at     
    android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)    
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at     
    android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)    
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at     
    android.view.View.performClick(View.java:4204)    
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at     
    android.view.View$PerformClick.run(View.java:17355)    
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at     
    android.os.Handler.handleCallback(Handler.java:725)    
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at  
    android.os.Handler.dispatchMessage(Handler.java:92)
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at
 android.os.Looper.loop(Looper.java:137   
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at    
    android.app.ActivityThread.main(ActivityThread.java:5041)    
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at     
    java.lang.reflect.Method.invokeNative(Native Method)    
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at    
    java.lang.reflect.Method.invoke(Method.java:511)    
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at    
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at 
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at dalvik.system.NativeStart.main(Native Method) 
    08-23 07:04:28.025: E/AndroidRuntime(24675): Caused by: java.lang.RuntimeException: Your   
    content must have a ListView whose id attribute is 'android.R.id.list'    
    08-23 07:04:28.025: E/AndroidRuntime(24675):    at     
    android.app.ListActivity.onContentChanged(ListActivity.java:243)

在下面提供我的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);
    setContentView(R.layout.activity_friends);
    //listview=(ListView)findViewById(R.id.list);

    openAndQueryDatabase();

    displayResultList();


    context=this;


    //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);

    //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        //   android.R.layout.simple_list_item_activated_1, android.R.id.text1, 
             value); 

    //listview.setAdapter(adapter);


    //listview.setOnItemClickListener(this);
         }


          private void setListAdapter(ArrayAdapter<String> arrayAdapter) {
    // TODO Auto-generated method stub

             }

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

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


                    }


        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) 
        {

            Intent in2=new   
                 Intent(Friends_list.this,InboxActivity.class);
            startActivity(in2);
        }


                    }

这是我用于显示列表视图的xml代码

         <LinearLayout  
          android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical"
              >
             <ListView
          android:id="@+id/list"
         style="@style/WhiteText"
              android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                android:paddingTop="10dp"
                >
            </ListView>  
           </LinearLayout>

4 个答案:

答案 0 :(得分:2)

id的{​​{1}}应为listview

所以你的xml看起来像:

android:id="@android:id/list

答案 1 :(得分:0)

你指的是id为android.R.id.list,我刚刚在listview上测试过,但没有找到id。使用R.id.list我希望它有所帮助!

答案 2 :(得分:0)

由于您正在扩展ListActivity, 你可以这样更好地给出:

listView = getListView();

答案 3 :(得分:0)

简单的解决方案只是扩展了简单的Activity

public class Friends_list extends ListActivity
onCreate()

中的

          @Override
   protected void onCreate(Bundle savedInstanceState) {

      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_friends);
      listview=(ListView)findViewById(R.id.list);

     // other code here....
   }