在 Android 中用意图启动活动 - 为什么这有效?

发布于 2025-01-03 10:02:38 字数 5179 浏览 2 评论 0原文

以前我一直在查看现有代码以在选项卡中显示地图,这次我尝试自己编写它来看看我是否理解它。

它运行良好,没有错误(屏幕截图),但我不完全了解为什么,以及我是否以最好的方式这样做。

我不太明白的部分在清单中。 我本以为 android:label 应该是 maptabview_name,但这给了我一个错误,说没有找到匹配的资源。

为什么在该活动中使用 app_name 时它会运行? 为什么找不到maptabview_name资源?

另外,在 MapTab2 中,这是启动意图的最佳方式吗? 这究竟告诉系统什么? “我打算从这堂课开始一项活动”?

这是我的代码 (它基于 这个):

MapTab2.java

package com.test.maptab2;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

public class MapTab2 extends TabActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent i = new Intent(this,MapTabView.class);

        TabHost.TabSpec spec;

        spec = getTabHost().newTabSpec("tab1");
        spec.setContent(i);
        spec.setIndicator("Map");
        getTabHost().addTab(spec);

        spec = getTabHost().newTabSpec("tab2");
        spec.setContent(R.id.detailstub);
        spec.setIndicator("Detail");
        getTabHost().addTab(spec);

        getTabHost().setCurrentTab(0);

    }

}

MapTabView.java

package com.test.maptab2;

import android.os.Bundle;
import com.google.android.maps.MapActivity;

public class MapTabView extends MapActivity {

    @Override
    public void onCreate (Bundle icicle){
        super.onCreate(icicle);
        setContentView(R.layout.maptabview);
    }

    @Override
    public boolean isRouteDisplayed(){
        return false;
    }

}

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content">

        <!-- Tab-switch panel -->
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

        <!-- Tab contents -->
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <!-- Map here -->
            <RelativeLayout android:id="@+id/mapstub"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>

            <!-- Other stuff -->
            <TextView android:id="@+id/detailstub"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="detail"/>

        </FrameLayout>  
    </LinearLayout>
</TabHost>

maptabview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/maptablayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.google.android.maps.MapView android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:apiKey="0HRMcD5o6WrBVhmwbWpeyeavZ67PXWOvJeeCx2g"/>

</RelativeLayout>

MapTab2.Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.maptab2"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

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

        <!--  Main  -->
        <activity android:name=".MapTab2"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!--  Map  -->
        <activity android:name=".MapTabView"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.EMBED" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Previously I've been looking at existing code to display a map in a tab, and this time I've tried to write it myself to see if I understood it.

It runs fine without errors (screenshot), but I don't completely understand why, and whether I'm doing this in the best way.

The part that I don't quite understand is in the manifest.
I would have thought that android:label should be maptabview_name, but that gives me an error saying no matching resource was found.

Why does it run when using app_name for that activity?
Why can't it find the maptabview_name resource?

Also, in MapTab2, is this the best way to start an intent?
What exactly is this telling the system? "I intend to start an activity from this class"?

Here's my code
(It's based on this):

MapTab2.java

package com.test.maptab2;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

public class MapTab2 extends TabActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent i = new Intent(this,MapTabView.class);

        TabHost.TabSpec spec;

        spec = getTabHost().newTabSpec("tab1");
        spec.setContent(i);
        spec.setIndicator("Map");
        getTabHost().addTab(spec);

        spec = getTabHost().newTabSpec("tab2");
        spec.setContent(R.id.detailstub);
        spec.setIndicator("Detail");
        getTabHost().addTab(spec);

        getTabHost().setCurrentTab(0);

    }

}

MapTabView.java

package com.test.maptab2;

import android.os.Bundle;
import com.google.android.maps.MapActivity;

public class MapTabView extends MapActivity {

    @Override
    public void onCreate (Bundle icicle){
        super.onCreate(icicle);
        setContentView(R.layout.maptabview);
    }

    @Override
    public boolean isRouteDisplayed(){
        return false;
    }

}

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content">

        <!-- Tab-switch panel -->
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

        <!-- Tab contents -->
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <!-- Map here -->
            <RelativeLayout android:id="@+id/mapstub"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>

            <!-- Other stuff -->
            <TextView android:id="@+id/detailstub"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="detail"/>

        </FrameLayout>  
    </LinearLayout>
</TabHost>

maptabview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/maptablayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.google.android.maps.MapView android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:apiKey="0HRMcD5o6WrBVhmwbWpeyeavZ67PXWOvJeeCx2g"/>

</RelativeLayout>

MapTab2.Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.maptab2"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

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

        <!--  Main  -->
        <activity android:name=".MapTab2"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!--  Map  -->
        <activity android:name=".MapTabView"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.EMBED" />
            </intent-filter>
        </activity>

    </application>
</manifest>

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

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

发布评论

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

评论(1

与之呼应 2025-01-10 10:02:38

Q1:maptabview_name 必须位于字符串资源文件中,通常位于 res/values/string.xml 中,以防万一您有它但它仍然不在生成的资源中文件(gen/package/R.java),尝试删除它并让 eclipse 再次生成它。

Q2:据我所知,TabHost.TabSpec.setContent(Intent Intent) 这就像在指定选项卡中启动 Activity 一样。我认为这就是处理选项卡时的做法。由于 Intent 用于做很多事情,因此这是基本用法,在本例中只是指定类。

Q1: maptabview_name has to be in a string resource file, usually at res/values/string.xml, in case you have it there and still it's not in the resources generated file (gen/package/R.java), try removing it and let eclipse generate it again.

Q2: AFAIK, TabHost.TabSpec.setContent(Intent intent) it's like starting the Activity within the specified tab. I think that's the way do do it when you deal with tabs. Since Intents are used to do a lot of things, this is a basic usage, in this case just to specify the class.

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