2013-02-05 55 views
-2

因爲我們知道選項卡活動現在已被棄用,所以一些教程說我使用FragmentActivity代替。我正在使用這個,它給我這個空行指針異常片段活動中的空指針異常?

tabHost.addTab(spec); 

我的代碼如下。

//MainActivity.java

public class MainActivity extends FragmentActivity { 
public final static int HOME = 1; 
public final static int CAMPUS = 2; 
public final static int REPORTING = 3; 
public final static int HELP = 4; 
public final static int WISH = 5; 
long transactionID = -1; 
public static TabHost tabHost; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mytabs); 
    MyView view = null; 

    tabHost = (TabHost)findViewById(R.id.my_tabhost); 
    TabHost.TabSpec spec; 
    Intent intent; 

    intent = new Intent().setClass(this, TabGroup1Activity.class); 
    view = new MyView(this, R.drawable.home, R.drawable.home_blue, ""); 
    view.setFocusable(true); 
    spec = tabHost.newTabSpec("Home").setIndicator(view).setContent(intent); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, TabGroup2Activity.class); 
    view = new MyView(this, R.drawable.home, R.drawable.home_blue, ""); 
    view.setFocusable(true); 
    spec = tabHost.newTabSpec("Campus").setIndicator(view) 
      .setContent(intent); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, TabGroup3Activity.class); 
    view = new MyView(this, R.drawable.home, R.drawable.home_blue, ""); 
    view.setFocusable(true); 
    spec = tabHost.newTabSpec("Reporting").setIndicator(view) 
      .setContent(intent); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, TabGroup4Activity.class); 
    view = new MyView(this, R.drawable.home, R.drawable.home_blue, ""); 
    view.setFocusable(true); 
    spec = tabHost.newTabSpec("Help").setIndicator(view) 
      .setContent(intent); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, TabGroup5Activity.class); 
    view = new MyView(this, R.drawable.home, R.drawable.home_blue, ""); 
    view.setFocusable(true); 
    spec = tabHost.newTabSpec("Wish").setIndicator(view).setContent(intent); 
    tabHost.addTab(spec); 

    tabHost.setCurrentTab(0); 
    tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = LayoutParams.WRAP_CONTENT; 
    tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = LayoutParams.WRAP_CONTENT; 
    tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = LayoutParams.WRAP_CONTENT; 
    tabHost.getTabWidget().getChildAt(3).getLayoutParams().height = LayoutParams.WRAP_CONTENT; 
    tabHost.getTabWidget().getChildAt(4).getLayoutParams().height = LayoutParams.WRAP_CONTENT; 

    tabHost.getTabWidget().getChildAt(0).getLayoutParams().width = LayoutParams.WRAP_CONTENT; 
    tabHost.getTabWidget().getChildAt(1).getLayoutParams().width = LayoutParams.WRAP_CONTENT; 
    tabHost.getTabWidget().getChildAt(2).getLayoutParams().width = LayoutParams.WRAP_CONTENT; 
    tabHost.getTabWidget().getChildAt(3).getLayoutParams().width = LayoutParams.WRAP_CONTENT; 
    tabHost.getTabWidget().getChildAt(4).getLayoutParams().width = LayoutParams.WRAP_CONTENT; 

    if (MyApp.getFrom().equals("Home")) { 
     tabHost.setCurrentTab(0); 
    } else if (MyApp.getFrom().equals("Campus")) { 
     tabHost.setCurrentTab(1); 
    } else if (MyApp.getFrom().equals("Reporting")) { 
     tabHost.setCurrentTab(2); 
    } else if (MyApp.getFrom().equals("Help")) { 
     tabHost.setCurrentTab(3); 
    } else if (MyApp.getFrom().equals("Wish")) { 
     tabHost.setCurrentTab(4); 
    } 

    int type = 0; 
    if (getIntent().getExtras() != null) { 
     if (getIntent().getExtras().containsKey("from")) { 
      type = getIntent().getExtras().getInt("from"); 
      switch (type) { 
      case HOME: 
       tabHost.setCurrentTab(0); 
      case CAMPUS: 
       tabHost.setCurrentTab(1); 
      case REPORTING: 
       tabHost.setCurrentTab(2); 
      case HELP: 
       tabHost.setCurrentTab(3); 
      case WISH: 
       tabHost.setCurrentTab(4); 
      default: 
       tabHost.setCurrentTab(0); 
      } 
     } 
    } 
} 

public long getTransactionID() { 
    return transactionID; 
} 

public void setTransactionID(long l) { 
    transactionID = l; 
} 

public void switchTabSpecial(int tab) { 
    tabHost.setCurrentTab(tab); 
} 

class ChangeTabReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     tabHost = (TabHost)findViewById(R.id.my_tabhost); 
     tabHost.setCurrentTab(1); 
    } 
} 

private class MyView extends LinearLayout { 
    ImageView iv; 

    public MyView(Context c, int drawable, int drawableselec, String label) { 
     super(c); 

     iv = new ImageView(c); 
     StateListDrawable listDrawable = new StateListDrawable(); 
     listDrawable.addState(SELECTED_STATE_SET, this.getResources() 
       .getDrawable(drawable)); 
     listDrawable.addState(ENABLED_STATE_SET, this.getResources() 
       .getDrawable(drawableselec)); 
     iv.setImageDrawable(listDrawable); 
     iv.setBackgroundColor(Color.TRANSPARENT); 
     iv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT, (float) 0.0)); 
     setGravity(Gravity.CENTER); 
     addView(iv); 
    } 
} 

}

//mytabs.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TabHost 
    android:id="@+id/my_tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:padding="0dp" > 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:padding="0dp" > 
     </FrameLayout> 

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

//錯誤

02-05 10:57:22.301: E/AndroidRuntime(911): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ccc.loyolacollege/com.ccc.loyolacollege.MainActivity}: java.lang.NullPointerException 
02-05 10:57:22.301: E/AndroidRuntime(911): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
02-05 10:57:22.301: E/AndroidRuntime(911): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
02-05 10:57:22.301: E/AndroidRuntime(911): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
02-05 10:57:22.301: E/AndroidRuntime(911): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
02-05 10:57:22.301: E/AndroidRuntime(911): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-05 10:57:22.301: E/AndroidRuntime(911): at android.os.Looper.loop(Looper.java:137) 
02-05 10:57:22.301: E/AndroidRuntime(911): at android.app.ActivityThread.main(ActivityThread.java:4424) 
02-05 10:57:22.301: E/AndroidRuntime(911): at java.lang.reflect.Method.invokeNative(Native Method) 
02-05 10:57:22.301: E/AndroidRuntime(911): at java.lang.reflect.Method.invoke(Method.java:511) 
02-05 10:57:22.301: E/AndroidRuntime(911): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
02-05 10:57:22.301: E/AndroidRuntime(911): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
02-05 10:57:22.301: E/AndroidRuntime(911): at dalvik.system.NativeStart.main(Native Method) 
02-05 10:57:22.301: E/AndroidRuntime(911): Caused by: java.lang.NullPointerException 
02-05 10:57:22.301: E/AndroidRuntime(911): at android.widget.TabHost.addTab(TabHost.java:229) 
02-05 10:57:22.301: E/AndroidRuntime(911): at com.ccc.loyolacollege.MainActivity.onCreate(MainActivity.java:38) 
02-05 10:57:22.301: E/AndroidRuntime(911): at android.app.Activity.performCreate(Activity.java:4465) 
02-05 10:57:22.301: E/AndroidRuntime(911): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
02-05 10:57:22.301: E/AndroidRuntime(911): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
+0

您調試了您的應用程序嗎?哪個變量是空的? – dilix

+0

查看我的回答,我已更新它並嘗試使用我的代碼。 – GrIsHu

回答

0

您尚未初始化TabHost。在你的onCreate()中添加下面的行。

tabHost = getTabHost(); 

TabHost ID也改變了佈局文件android:id="@android:id/tabhost"

看看我的下面的佈局。

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" > 
    <TabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:padding="0dp" > 
    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:padding="0dp" > 
    </FrameLayout> 
    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center|center" 
     android:tabStripEnabled="false"/>  
</LinearLayout> 
    </TabHost> 
</LinearLayout> 

而且在你活動的延伸,而不是TabActivityFragmentActivity。然後運行應用程序。

我希望它能幫助你。

謝謝。