2012-02-07 4 views
1

以前は、タブにマップを表示するための既存のコードを見てきましたが、今回はそれを自分で作成して理解したかどうかを確認しようとしました。Androidでのインテントによるアクティビティの開始 - これはなぜ機能するのですか?

エラーなしで正常に動作しますが(screenshot)、私はこれを最善の方法で実行しているのかどうかは完全にはわかりません。

わかりにくい部分はマニフェストにあります。 私はアンドロイド:ラベルがmaptabview_nameでなければならないと思っていましたが、それは私に合ったリソースが見つからなかったというエラーを返します。

そのアクティビティにapp_nameを使用すると、なぜそれが実行されますか? maptabview_nameリソースが見つからないのはなぜですか?

また、MapTab2では、これをインテントを開始する最も良い方法ですか? これはシステムに正確に何を伝えていますか? "私はこのクラスから活動を始めるつもりですか?"ここで

は、(それが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> 

答えて

0

Q1:maptabview_nameは、通常res/values/string.xmlで、文字列リソースファイルにしておく必要があります場合は、あなたがそこにそれを持っているし、まだそれは資源ではありません生成されたファイル(gen/package/R.java)を削除して、それを再度生成させるようにしてください。

Q2:AFAIK、TabHost.TabSpec.setContent(Intent intent)これは、指定したタブ内のアクティビティの開始に似ています。私はそれがあなたがタブを扱うときにそれをする方法だと思う。インテントは多くのことを行うために使用されるので、これは基本的な使い方です。この場合、クラスを指定するだけです。

+0

ああ!ありがとうございます。私はR.javaのように自動的に処理されたと想定しています。 – Ephemeros

関連する問題