Android 地图到选项卡

发布于 2024-10-31 14:05:17 字数 3696 浏览 4 评论 0原文

嘿,我尝试了很多创建选项卡的方法,结果一切顺利,尽管当我向其中添加地图时它崩溃了,我尝试了这个人在这里所做的事情:http://joshclemm.com/blog/?p=86

我的应用程序在出现任何选项卡之前崩溃了。代码如下:

import android.content.Intent; 导入 android.graphics.Color; 导入 android.os.Bundle; 导入 android.view.View; 导入 android.widget.TabHost; 导入 android.widget.TabHost.OnTabChangeListener; 导入 android.widget.TabHost.TabContentFactory;

导入 com.google.android.maps.MapActivity; 导入 com.google.android.maps.MapView;

public class Tabs extends MapActivity Implements OnTabChangeListener {

private static final String HOME_TAG = "Home";
private static final String PLAYERS_TAG = "Players";
private static final String MAP_TAG = "Map";
private static final String TICKET_TAG = "Ticket";

private TabHost tabHost;
private MapView mapView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tabHost = (TabHost)findViewById(android.R.id.tabhost);
    tabHost.setup();
    tabHost.setOnTabChangedListener(this);

    mapView = (MapView)findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapView.postInvalidate();

    tabHost.addTab(tabHost.newTabSpec(HOME_TAG).setIndicator("Home").setContent(new Intent(this, Home.class)));
    tabHost.addTab(tabHost.newTabSpec(PLAYERS_TAG).setIndicator("Players").setContent(new Intent(this, Players.class)));
    tabHost.addTab(tabHost.newTabSpec(MAP_TAG).setIndicator("Venue").setContent(new TabContentFactory () {

        public View createTabContent(String arg0) {
            return mapView;
        }
    }));

    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
        tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#00CC33"));
    }

    tabHost.getTabWidget().setCurrentTab(0);
    tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#3399FF"));
}

public void onTabChanged(String tabId) {
    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
        {
            tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#00CC33"));
        }
    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#3399FF"));
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

}

我的清单:

<application android:icon="@drawable/englandrugbyrose" 
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen">
    <activity android:name=".Splash"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Tabs" />
    <activity android:name=".Home" />
    <activity android:name=".Players" />
    <activity android:name=".Tickets" />
    <activity android:name=".Map" />

    <uses-library android:name="com.google.android.maps" />

</application>
    <uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />

顺便说一句,我的 XML 位于不同的 XML 文件中,例如包含用于加载内容的 mapview id 的 map.xml,这是我的 XML 与其他人的 XML 之间的唯一区别。

更新: logcat

它指向 mapView.setBuiltInZoomControls(true); 处的第 35 行;有空指针异常

谢谢。

Hey I have tried a lot of ways of creating tabs which went ok, although when I add a map to it it crashes, I tried what this guy did here: http://joshclemm.com/blog/?p=86

My application crashes before any tabs appear. The code is below:

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class Tabs extends MapActivity implements OnTabChangeListener {

private static final String HOME_TAG = "Home";
private static final String PLAYERS_TAG = "Players";
private static final String MAP_TAG = "Map";
private static final String TICKET_TAG = "Ticket";

private TabHost tabHost;
private MapView mapView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tabHost = (TabHost)findViewById(android.R.id.tabhost);
    tabHost.setup();
    tabHost.setOnTabChangedListener(this);

    mapView = (MapView)findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapView.postInvalidate();

    tabHost.addTab(tabHost.newTabSpec(HOME_TAG).setIndicator("Home").setContent(new Intent(this, Home.class)));
    tabHost.addTab(tabHost.newTabSpec(PLAYERS_TAG).setIndicator("Players").setContent(new Intent(this, Players.class)));
    tabHost.addTab(tabHost.newTabSpec(MAP_TAG).setIndicator("Venue").setContent(new TabContentFactory () {

        public View createTabContent(String arg0) {
            return mapView;
        }
    }));

    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
        tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#00CC33"));
    }

    tabHost.getTabWidget().setCurrentTab(0);
    tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#3399FF"));
}

public void onTabChanged(String tabId) {
    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
        {
            tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#00CC33"));
        }
    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#3399FF"));
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

}

And my manifest:

<application android:icon="@drawable/englandrugbyrose" 
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen">
    <activity android:name=".Splash"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Tabs" />
    <activity android:name=".Home" />
    <activity android:name=".Players" />
    <activity android:name=".Tickets" />
    <activity android:name=".Map" />

    <uses-library android:name="com.google.android.maps" />

</application>
    <uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />

Btw, my XML are in different XML files such as map.xml that contains the mapview id for loading the content, thats the only difference between mine and that other guys XML.

UPDATE: logcat

It points to line 35 at the mapView.setBuiltInZoomControls(true); With a nullpointer exception

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时模样 2024-11-07 14:05:17

您是否尝试过降级到 API 级别 7?当我处于 API 级别 8 时,我遇到了 mapView.setBuiltInZoom(true) 的问题。当我更改为 API 级别 7 时,异常停止了。

Have you tried downgrading to API level 7? I had that problem with the mapView.setBuiltInZoom(true) when I was on API level 8. The exception stopped when I changed to API level 7.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文