片段活动中的空指针异常?

时间:2013-02-05 05:28:58

标签: android android-tabhost

我们知道tab现在不推荐使用Activity,因此有些教程说我使用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)

1 个答案:

答案 0 :(得分: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>

在您的活动中扩展TabActivity而不是FragmentActivity。然后运行您的应用。

我希望它会对你有所帮助。

感谢。