TabHost.addTab不起作用

时间:2014-11-30 12:32:06

标签: android android-tabhost

我有三个活动,其中一个有地图片段,它就是启动活动。我正在尝试使用TabHost来浏览它们。我的问题是,当尝试使用addTab()添加标签时,应用程序崩溃了。 logcat说“无法启动活动ComponentInfo ......”。

这是我的主要活动,它有一个地图片段(tabhost代码来自另一个教程):     公共类MainActivity扩展TabActivity {

private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    db =new DBHelper(this);
    // get the reference of the map view
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
    // enable map to have the current location
    map.setMyLocationEnabled(true);
    map.setMapType(1);

    //make marker to point to the main festival area, set title and subtitle
    Marker festArea = map.addMarker(new MarkerOptions().position(FESTIVAL_AREA)
            .title("Panther Plaza").snippet("Main location"));

    //go to the festival area
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(FESTIVAL_AREA,1));

    //zoom this much to  the festival area
    map.animateCamera(CameraUpdateFactory.zoomTo(18), 2000, null);

    TabHost tabHost = getTabHost();

    // Tab for Photos
    TabHost.TabSpec photospec = tabHost.newTabSpec("Photos");
    // setting Title and Icon for the Tab
    photospec.setIndicator("Songs");
    Intent photosIntent = new Intent(this, ProgramMainActivity.class);
    photospec.setContent(photosIntent);

    // Tab for Songs
    TabHost.TabSpec songspec = tabHost.newTabSpec("Songs");
    songspec.setIndicator("Songs");
    Intent songsIntent = new Intent(this, FITInternationalFestMainActivity.class);
    songspec.setContent(songsIntent);


    // Adding all TabSpec to TabHost
   //this is not working 
    tabHost.addTab(photospec); // Adding photos tab
    tabHost.addTab(songspec); // Adding songs tab


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.avtitiy_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

这是我的activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">


<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment"
    tools:layout="@layout/activity_main" />

<ImageView
    android:id="@+id/currentLocation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="400dp"
    android:onClick="toCurrentLocation"
    android:src="@drawable/location" />
<ImageView
    android:id="@+id/carLocation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="420dp"
    android:layout_marginLeft="60dp"
    android:onClick="myCarParking"
    android:src="@drawable/car" />
<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"/>
    </LinearLayout>
</TabHost>

我正在努力解决这个问题,我无法让它发挥作用,应用总是失败。

这是logcat:

 11-30 07:13:01.395  24345-24345/ E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: edu.fit.alharbia2013.fitinternationalfest, PID: 24345
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.user.mainactivity/com.company.user.myfestival.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
            at android.content.res.Resources.getValue(Resources.java:1123)
            at android.content.res.Resources.loadXmlResourceParser(Resources.java:2309)
            at android.content.res.Resources.getLayout(Resources.java:939)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
            at android.widget.TabHost$LabelIndicatorStrategy.createIndicatorView(TabHost.java:598)
            at android.widget.TabHost.addTab(TabHost.java:227)
            at com.company.user.mainactivity.MyFestival.onCreate(MainActivity.java:112)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
              at android.app.ActivityThread.access$800(ActivityThread.java:135)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
               at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

          

0 个答案:

没有答案