内TabHost Android地图。 getTabHost()返回错误 [英] Android Maps within TabHost. getTabHost() return error

查看:198
本文介绍了内TabHost Android地图。 getTabHost()返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题初始化TabHost。我需要在浏览几个标签这显示出不同的活动:一个必须显示谷歌地图,第二 - 登录表单。在创建code有错误的方法getTabHost()是未定义的类型MapViewDemo

 包com.example.android.apis.view;进口的java.util.List;
导入android.app *。
进口com.example.android.google.apis.R;
进口com.google.android.maps.GeoPoint;
进口com.google.android.maps.MapActivity;
进口com.google.android.maps.MapController;
进口com.google.android.maps.MapView;
进口com.google.android.maps.Overlay;
进口com.google.android.maps.OverlayItem;
进口android.widget.TabHost;
进口android.content.Intent;
进口android.content.res.Resources;
进口android.graphics.drawable.Drawable;
进口android.os.Bundle;
导入android.widget *。
进口com.example.android.apis.view.GMapManager;公共类MapViewDemo扩展MapActivity {    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.mapview);
        MapView的mMapView =(图形页面)findViewById(R.id.mapview);        mMapView.setBuiltInZoomControls(真);
        MapController mMapController = mMapView.getController();
        双X,Y;
        X = 59.434034;
        Y = 24.757687;
        双[] = XY {X,Y};
        的GeoPoint p值= coordinatesToGeoPoint(XY);
        mMapController.animateTo(P);
        mMapController.setZoom(18);
        mMapView.invalidate();
        mMapView.setBuiltInZoomControls(真);        清单<&叠加GT; mapOverlays = mMapView.getOverlays();
        可绘制可绘制= this.getResources()getDrawable(R.drawable.marker_green)。
        HelloItemizedOverlay itemizedoverlay =新HelloItemizedOverlay(绘制);        的GeoPoint点= P;
        OverlayItem overlayitem =新OverlayItem(点,123!,);        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);        资源解析度= getResources(); //资源对象获取可绘制
        TabHost tabHost = getTabHost(); //活动TabHost
        TabHost.TabSpec规范; // Resusable则tabspec为每个标签
        意图意图; //可重用的意图为每个标签        //做其他选项卡相同
                意图=新意图()setClass(这一点,GMapManager.class)。
                规格= tabHost
                        .newTabSpec(经理)
                        .setIndicator(经理人,
                                res.getDrawable(R.drawable.ic_tabs))
                        .setContent(意向);
                tabHost.addTab(规范);                意图=新意图()setClass(这一点,MapViewDemo.class)。
                规格= tabHost
                        .newTabSpec(图)
                        .setIndicator(地图,res.getDrawable(R.layout.mapview))
                        .setContent(意向);
                tabHost.addTab(规范);                tabHost.setCurrentTab(2);    }    @覆盖
    保护布尔isRouteDisplayed(){返回false; }
    / **
     *一对坐标转换为GeoPoint对象
     *
     * @参数COORDS双重包含经度和纬度
     * @返回GeoPoint对象为同一COORDS
     * /
    公共静态的GeoPoint coordinatesToGeoPoint(双[] coords)使用{
        如果(coords.length→2){
            返回null;
        }
        如果(COORDS [0] == Double.NaN || COORDS [1] == Double.NaN){
            返回null;
        }
        最终诠释纬度=(int)的(COORDS [0] * 1E6);
        最终诠释经度=(int)的(COORDS [1] * 1E6);
        返回新的GeoPoint(经度,纬度);
    }}

Eclipse中给了我创造的方法getTabHost()。我接受。 Eclipse中创造了它,但我没有看到它在我目前的类中的任何变化。现在,它可以编译,但产生的RuntimeException。这里是堆栈跟踪:

 发[< 1 GT;主](吊(RuntimeException的除外))
    ActivityThread.performLaunchActivity(ActivityThread $ ActivityClientRecord,意图)线:1955
    ActivityThread.handleLaunchActivity(ActivityThread $ ActivityClientRecord,意图)线:1980
    ActivityThread.access $ 600(ActivityThread,ActivityThread $ ActivityClientRecord,意图)线:122
    ActivityThread $ H.handleMessage(消息)行:1146
    ActivityThread $ H(处理器).dispatchMessage(消息)行:99
    Looper.loop()行:137
    ActivityThread.main(字符串[])行:4340
    Method.invokeNative(对象,对象[],上课,下课[],类,整型,布尔)行:不可用[本机方法]
    Method.invoke(对象,对象...)线:511
    ZygoteInit $ MethodAndArgsCaller.run()行:784
    ZygoteInit.main(字符串[])线:551
    NativeStart.main(字符串[])行:不可用[本机方法]

这是我的layout.xml

 < TabHost的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@机器人:ID / tabhost
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>    <的LinearLayout
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:ID =@ + ID /主
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直
        机器人:填充=5DP>        < com.google.android.maps.MapView
            机器人:ID =@ + ID /图形页面
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:apiKey =03xx7gYjYkcIs5nDM_44v02HgLCmO3Bcega19yA
            机器人:可点击=真
            机器人:启用=真/>
        <的TextView
            的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:填充=10dp
            机器人:TEXTSIZE =16SP>
        < / TextView的>        < TabWidget
            机器人:ID =@机器人:ID /标签
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT/>        <的FrameLayout
            机器人:ID =@机器人:ID / tabcontent
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:填充=5DP/>
    < / LinearLayout中>< / TabHost>


解决方案

getTabHost()是方法的中找到 TabActivity

如果你想添加得到比 TabActivity 其他选项卡,把TabHost在layout.xml,然后尝试这种方式:

  TabHost tabHost =(TabHost)this.findViewById(R.id.tab_host_id);
tabHost.setup(); //不要忘记打电话给该行。

有关详细去 href=\"http://stackoverflow.com/q/3163884/593709\">

I have problem with initializing TabHost. I need to have in View several Tabs which show different activities: one must show google map, second - log in form. In created code there is error "The method getTabHost() is undefined for the type 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 offered me to create method getTabHost(). I accepted. Eclipse created it, but I don't see it in my current class any changes. Now it can compile but produces RuntimeException. Here is stack trace:

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]

And here is my 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>

解决方案

getTabHost() is method only found in TabActivity

and if you want to add get Tabs other than TabActivity, put TabHost in layout.xml and then try this way:

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

For more detail go here

这篇关于内TabHost Android地图。 getTabHost()返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆