TabHost中的Android地图。 getTabHost()返回错误

时间:2011-11-01 13:37:50

标签: android android-mapview android-tabhost

我在初始化TabHost时遇到问题。我需要在View中显示几个显示不同活动的标签:一个必须显示谷歌地图,第二个 - 登录表格。在创建的代码中有错误“方法getTabHost()未定义类型MapViewDemo”

package com.example.android.apis.view;

import java.util.List;
import android.app.*;
import com.example.android.google.apis.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import android.widget.TabHost;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.*;
import com.example.android.apis.view.GMapManager;

public class MapViewDemo extends MapActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview);
        MapView mMapView = (MapView) findViewById(R.id.mapview);

        mMapView.setBuiltInZoomControls(true);
        MapController mMapController = mMapView.getController();
        double x, y;
        x = 59.434034;
        y = 24.757687;
        double[] xy = {x,y};
        GeoPoint p = coordinatesToGeoPoint(xy);
        mMapController.animateTo(p);
        mMapController.setZoom(18);
        mMapView.invalidate();
        mMapView.setBuiltInZoomControls(true);

        List<Overlay> mapOverlays = mMapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.marker_green);
        HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);

        GeoPoint point = p;
        OverlayItem overlayitem = new OverlayItem(point, "123!", "");

        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost(); // The activity TabHost
        TabHost.TabSpec spec; // Resusable TabSpec for each tab
        Intent intent; // Reusable Intent for each tab

        // Do the same for the other tabs
                intent = new Intent().setClass(this, GMapManager.class);
                spec = tabHost
                        .newTabSpec("manager")
                        .setIndicator("Manager",
                                res.getDrawable(R.drawable.ic_tabs))
                        .setContent(intent);
                tabHost.addTab(spec);

                intent = new Intent().setClass(this, MapViewDemo.class);
                spec = tabHost
                        .newTabSpec("map")
                        .setIndicator("Map", res.getDrawable(R.layout.mapview))
                        .setContent(intent);
                tabHost.addTab(spec);

                tabHost.setCurrentTab(2);



    }

    @Override
    protected boolean isRouteDisplayed() { return false; }
    /**
     * Converts a pair of coordinates to a GeoPoint
     * 
     * @param coords double containing latitude and longitude
     * @return GeoPoint for the same coords
     */
    public static GeoPoint coordinatesToGeoPoint(double[] coords) {
        if (coords.length > 2) {
            return null;
        }
        if (coords[0] == Double.NaN || coords[1] == Double.NaN) {
            return null;
        }
        final int latitude = (int) (coords[0] * 1E6);
        final int longitude = (int) (coords[1] * 1E6);
        return new GeoPoint(latitude, longitude);
    }

}

Eclipse让我创建方法getTabHost()。我接受。 Eclipse创建了它,但我在当前的类中没有看到任何更改。现在它可以编译但生成RuntimeException。这是堆栈跟踪:

Thread [<1> main] (Suspended (exception RuntimeException))  
    ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1955    
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1980 
    ActivityThread.access$600(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 122    
    ActivityThread$H.handleMessage(Message) line: 1146  
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 137 
    ActivityThread.main(String[]) line: 4340    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 511  
    ZygoteInit$MethodAndArgsCaller.run() line: 784  
    ZygoteInit.main(String[]) line: 551 
    NativeStart.main(String[]) line: not available [native method]

这是我的layout.xml

<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
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="5dp" >

        <com.google.android.maps.MapView
            android:id="@+id/mapview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:apiKey="03xx7gYjYkcIs5nDM_44v02HgLCmO3Bcega19yA"
            android:clickable="true"
            android:enabled="true" />


        <TextView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="10dp"
            android:textSize="16sp" >
        </TextView>

        <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"
            android:padding="5dp" />
    </LinearLayout>

</TabHost>

1 个答案:

答案 0 :(得分:3)

getTabHost()是在TabActivity

中找到的方法

如果你想添加除TabActivity以外的获取标签,请将TabHost放在layout.xml中,然后尝试这种方式:

TabHost tabHost = (TabHost)this.findViewById(R.id.tab_host_id);
tabHost.setup(); // dont forget to call this line.

有关详细信息,请转到here