2011-09-13 9 views
0

我創建了一個活動與一個不可見的TabHost,它從我的數據庫填充ListActivity獲取的內容。 如果我打開我的活動與其中的TabHost,我收到強制關閉錯誤。問題可能在我的ListActivity中。如果TabHost訪問ListActivity - >空指針異常

這裏是我的活動與Tabhost的代碼:現在,這裏

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


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabcontent" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

     <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"> 

     <RelativeLayout 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" /> 
       <TabWidget 
        android:id="@android:id/tabs" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignBottom = "@android:id/tabcontent" 
        android:visibility="invisible"/> 

        <TableRow 
         android:layout_height="wrap_content" 
         android:id="@+id/ADFilterOverview" 
         android:layout_width="match_parent" 
         android:onClick="onClickADConfig" 
         android:layout_alignParentBottom="true" 
         android:layout_alignParentLeft="true"> 

         <TextView 
         android:layout_gravity="bottom|left" 
         android:text="@string/newadfilter" 
         style="@style/NormalFont"/> 
       </TableRow> 
     </RelativeLayout> 
     </TabHost> 
    </FrameLayout> 

你看我ListActivity的代碼:

package de.retowaelchli.filterit; 


import de.retowaelchli.filterit.stats.CreatedADFilters; 
import android.app.TabActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TabHost; 


public class ADeleteActivity extends TabActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.autodelete_test); 

     TabHost tabHost = getTabHost();  

     tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("All AutodeleteFilters").setContent(new Intent(this, CreatedADFilters.class))); 
     tabHost.setCurrentTab(0); 


    } 
    /** Verweise auf die anderen Seiten **/ 
    public void onClickADConfig(View view){ 
     final Intent i = new Intent(this, ADFilterConfigActivity.class); 
     startActivity(i); } 
} 

下面是它的佈局,文件

package de.retowaelchli.filterit.stats; 


import de.retowaelchli.filterit.R; 
import de.retowaelchli.filterit.database.ADFilterDBAdapter; 
import android.app.ListActivity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.widget.SimpleCursorAdapter; 


public class CreatedADFilters extends ListActivity { 

    //Variablen deklaration 
    private ADFilterDBAdapter mDbHelper; 

    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 

     mDbHelper=new ADFilterDBAdapter(this); 
     fillData(); 

    } 
    private void fillData() { 
     // Get all of the notes from the database and create the item list 
     Cursor c = mDbHelper.getAllADFilter(); 
     startManagingCursor(c); 

     String[] from = new String[] { mDbHelper.NAME }; 
     int[] to = new int[] { R.id.empty }; 

     // Now create an array adapter and set it to display using our row 
     SimpleCursorAdapter adname = 
      new SimpleCursorAdapter(this, R.layout.list_layout, c, from, to); 
     setListAdapter(adname); 
    } 
} 

這裏是它的佈局文件:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    style="@style/NormalFont" 
    android:id="@+id/empty" > 
</TextView> 

所以我在做什麼錯.. :(

預先感謝您。

問候

野生動物園

這裏的logcat的。

09-13 14:20:11.652: INFO/ActivityManager(1721): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10100000 cmp=de.retowaelchli.filterit/.StartseiteActivity } from pid 1721 
09-13 14:20:11.672: VERBOSE/HtcAppUsageStats(1721): (launch app, package): (Filter It, de.retowaelchli.filterit) 
09-13 14:20:11.672: DEBUG/StatusBarManagerService(1721): disable what =0, token = [email protected], pkg = android, mBar = [email protected] 
09-13 14:20:11.672: INFO/ActivityManager(1721): Start proc de.retowaelchli.filterit for activity de.retowaelchli.filterit/.StartseiteActivity: pid=3992 uid=10150 gids={} 
09-13 14:20:11.812: INFO/SuperPowerSvc(29815): [SuperPowerSvc] App launched: [de.retowaelchli.filterit] 
09-13 14:20:12.002: DEBUG/dalvikvm(1721): GC_EXTERNAL_ALLOC freed 1394K, 34% free 10500K/15815K, external 10208K/12256K, paused 147ms 
09-13 14:20:12.232: WARN/ResourceType(1721): Skipping entry 0x7f040007 in package table 0 because it is not complex! 
09-13 14:20:12.232: WARN/ResourceType(1721): Skipping entry 0x7f040008 in package table 0 because it is not complex! 
09-13 14:20:12.272: DEBUG/SurfaceFlinger(1721): layer=0xc46a48 is not in the purgatory list 
09-13 14:20:12.292: DEBUG/dalvikvm(3992): GC_EXTERNAL_ALLOC freed 44K, 50% free 2695K/5379K, external 0K/0K, paused 25ms 
09-13 14:20:12.442: WARN/InputManagerService(1721): [unbindCurrentClientLocked] Disable input method client. 
09-13 14:20:12.442: WARN/InputManagerService(1721): [startInputLocked] Enable input method client. 
09-13 14:20:12.512: INFO/ActivityManager(1721): Displayed de.retowaelchli.filterit/.StartseiteActivity: +837ms 
09-13 14:20:13.472: INFO/ActivityManager(1721): Starting activity: Intent { cmp=de.retowaelchli.filterit/.ADeleteActivity } from pid 3992 
09-13 14:20:13.502: INFO/SuperPowerSvc(29815): [SuperPowerSvc] App launched: [de.retowaelchli.filterit] 
09-13 14:20:13.622: DEBUG/dalvikvm(3992): GC_EXTERNAL_ALLOC freed 36K, 49% free 2769K/5379K, external 5760K/7192K, paused 22ms 
09-13 14:20:13.742: DEBUG/AndroidRuntime(3992): Shutting down VM 
09-13 14:20:13.742: WARN/dalvikvm(3992): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992): FATAL EXCEPTION: main 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.retowaelchli.filterit/de.retowaelchli.filterit.ADeleteActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{de.retowaelchli.filterit/de.retowaelchli.filterit.stats.CreatedADFilters}: java.lang.NullPointerException 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at android.app.ActivityThread.access$1500(ActivityThread.java:132) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at android.os.Handler.dispatchMessage(Handler.java:99) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at android.os.Looper.loop(Looper.java:143) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at android.app.ActivityThread.main(ActivityThread.java:4196) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at java.lang.reflect.Method.invoke(Method.java:507) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at dalvik.system.NativeStart.main(Native Method) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{de.retowaelchli.filterit/de.retowaelchli.filterit.stats.CreatedADFilters}: java.lang.NullPointerException 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:1651) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:656) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at android.widget.TabHost.setCurrentTab(TabHost.java:326) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at android.widget.TabHost.addTab(TabHost.java:216) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at de.retowaelchli.filterit.ADeleteActivity.onCreate(ADeleteActivity.java:22) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  ... 11 more 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992): Caused by: java.lang.NullPointerException 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at de.retowaelchli.filterit.database.ADFilterDBAdapter.getAllADFilter(ADFilterDBAdapter.java:108) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at de.retowaelchli.filterit.stats.CreatedADFilters.fillData(CreatedADFilters.java:26) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at de.retowaelchli.filterit.stats.CreatedADFilters.onCreate(CreatedADFilters.java:21) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780) 
09-13 14:20:13.742: ERROR/AndroidRuntime(3992):  ... 20 more 
09-13 14:20:13.752: WARN/ActivityManager(1721): Force finishing activity de.retowaelchli.filterit/.ADeleteActivity 
09-13 14:20:13.752: WARN/ActivityManager(1721): Force finishing activity de.retowaelchli.filterit/.StartseiteActivity 
09-13 14:20:14.262: WARN/ActivityManager(1721): Activity pause timeout for HistoryRecord{4058e8d8 de.retowaelchli.filterit/.ADeleteActivity} 
09-13 14:20:14.352: WARN/ResourceType(1721): Skipping entry 0x7f040006 in package table 0 because it is not complex! 
09-13 14:20:14.352: WARN/ResourceType(1721): Skipping entry 0x7f040005 in package table 0 because it is not complex! 

POST是用新的Logcat和overdone代碼更新的,從您的發佈!

回答

2

你是不是裝箱的對象爲ADFilterDBAdapter

public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 

    mDbHelper=new ADFilterDBAdapter(this); 
    fillData(); 

} 

和更多的,這是根據您的代碼,你只創建一個tab.so當前選項卡的索引設置爲零(索引從零開始)

tabHost.setCurrentTab(0); 
+0

嘿Balaji,我做了兩個改變你說,但我仍然得到ForceClose錯誤! :( – safari

+0

粘貼你的logcat錯誤信息.. –

+0

allready did。仍然NullPointerException某處 – safari

1

Set tabHost.setCurrentTab(0);而不是tabHost.setCurrentTab(1);

這就是「NullPointerException」的原因。

+0

嘗試過但仍然是空指針例外。 – safari

0

我發現了它,它是一個小數據庫問題表不存在。 thx爲您提供幫助:D