2011-05-18 17 views
1

我已经遵循了几个不同的教程,没有任何运气让这个工作。我可以看到的唯一区别是,我使用数据库从数据中提取数据,并在教程中创建数组以获取数据。带有2个选项卡和2个列表视图的Tabhost从数据库获取数据

以下是我正在使用的代码,以查看是否有人可以帮助我。

的main.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/tabhost" android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<LinearLayout android:orientation="vertical" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <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"> 
     <ListView android:layout_width="fill_parent" android:id="@+id/listView1" 
      android:layout_height="wrap_content"></ListView> 
     <ListView android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:id="@+id/listView2"> 
     </ListView> 
    </FrameLayout> 
</LinearLayout> 

main.java文件:

import android.app.TabActivity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListView; 
import android.widget.TabHost; 
import android.widget.TabHost.OnTabChangeListener; 
import android.widget.TabHost.TabContentFactory; 

import com.facebook.android.R; 

public class MainLib extends TabActivity implements OnTabChangeListener { 


private static final String TAG = "MainLib"; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mainlib); 

    //final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); 
    //tabHost.setup(); 
    TabHost mTabHost = getTabHost(); 
    mTabHost.setOnTabChangedListener(this); 

    // Find your views NEW LIBS 
    final ListView listPro = (ListView) findViewById(R.id.listView1); 

    final DBLibAdapter db = new DBLibAdapter(this); 

    db.open(); 

    final Cursor c = db.getAllNewLibs(); 
    startManagingCursor(c); 
    Log.d(TAG, "New Libs Cursor Created"); 

    // Create the adapter 
    NewLibAdapter adapter = new NewLibAdapter(this, c); 
    listPro.setAdapter(adapter); 
    Log.d(TAG, "New Libs Adapter Created"); 

    db.close(); 


    listPro.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      db.open(); 

      db.updateId(id); 

      db.close(); 

      Intent miamor = new Intent(MainLib.this, Result.class); 
      startActivity(miamor); 

     } 
    }); 

    // Find your views SAVED LIBS 
    final ListView listPro1 = (ListView) findViewById(R.id.listView2); 

    db.open(); 

    final Cursor c1 = db.getAllSavedLibs(); 
     startManagingCursor(c1); 

     Log.d(TAG, "Saved Libs Cursor Created"); 
    // Create the adapter 
    SavedLibAdapter adapter1 = new SavedLibAdapter(this, c1); 
    listPro1.setAdapter(adapter1); 

    Log.d(TAG, "Saved Libs Adapter Created"); 

    db.close(); 

    listPro1.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      db.open(); 

      db.updateId(id); 

      db.close(); 

      Intent amormio = new Intent(MainLib.this, Result.class); 
      startActivity(amormio); 

     } 
    }); 

    mTabHost.addTab(mTabHost.newTabSpec("New Libs").setIndicator("New Libs") 
      .setContent(new TabContentFactory(){ 

       @Override 
       public View createTabContent(String arg0) { 
        return listPro; 
       } 
      })); 

    mTabHost.addTab(mTabHost.newTabSpec("Saved Libs").setIndicator("Saved Libs") 
      .setContent(new TabContentFactory(){ 

       @Override 
       public View createTabContent(String tag) { 
        return listPro1; 
       } 
      })); 

    mTabHost.setCurrentTab(0); 
} 

    public void onTabChanged(String tabName) { 
     if(tabName.equals("New Libs")) { 
      //do something 
     } 
     else if(tabName.equals("Saved Libs")) { 
      //do something 
     } 
} 
} 

第一ListView中有数据,但是,第二个没有。我试图删除第二个,但它仍然会抛出一条错误消息:

05-18 16:57:30.964: ERROR/AndroidRuntime(32753): Uncaught handler: thread main exiting due to uncaught exception 
05-18 16:57:30.984: ERROR/AndroidRuntime(32753): java.lang.NullPointerException 
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):  at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:295) 
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):  at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661) 
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):  at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:295) 
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):  at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661) 
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):  at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661) 
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):  at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661) 
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1819) 
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):  at android.os.Looper.loop(Looper.java:123) 
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):  at android.app.ActivityThread.main(ActivityThread.java:4363) 
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):  at java.lang.reflect.Method.invoke(Method.java:521) 
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
05-18 16:57:30.984: ERROR/AndroidRuntime(32753):  at dalvik.system.NativeStart.main(Native Method) 

请帮忙!

+0

setContentView(R.layout.mainlib)不应该是setContentView(R.layout.main)? – yogsma 2011-05-18 21:00:00

回答

0

您在主布局中包含ListViews。您需要为每个活动创建一个布局。您正在按需创建布局,但随后在另一个布局(main)中引用listview。

查看Tab Layout教程:http://developer.android.com/resources/tutorials/views/hello-tabwidget.html查看示例。

+0

您应该管理[ListActivity](http://developer.android.com/reference/android/app/ListActivity.html)中的每个列表,而不是常规的活动 – Aleadam 2011-05-19 05:07:18

+0

这最终取得了令人满意的成果。感谢您的帮助!! – xappz 2011-05-19 05:07:22

+0

@xappz很棒:) – Aleadam 2011-05-19 05:07:55