2015-07-08 8 views
0

В моем текущем приложении я пытаюсь добавить вкладки. На одной из вкладок я хочу показать список, а на другой вкладке я хочу показать карту. В моей предыдущей версии приложения я показываю карту с поддержкой SupportMapFragment. Поэтому я все еще хочу использовать это (не хочу менять слишком много кода). Но когда я пытаюсь добавить mapfragment, он показывает ошибку. Может ли кто-нибудь сказать мне, как я могу это достичь?SupportMapFragment в TabHost, Android

Вот мой 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="fill_parent"> 
    <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"> 

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      > 
      <RadioGroup 
       android:id="@+id/rg_views" 
       android:orientation="horizontal" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentEnd="true"> 

       <RadioButton 
        android:id="@+id/rb_normal" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/str_rb_normal" 
        android:checked="true" /> 

       <RadioButton 
        android:id="@+id/rb_satellite" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/str_rb_satellite" /> 

       <RadioButton 
        android:id="@+id/rb_terrain" 
        android:layout_width="161dp" 
        android:layout_height="wrap_content" 
        android:text="@string/str_rb_terrain" /> 

      </RadioGroup> 
      <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/map" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_below="@id/rg_views" 
       class="com.google.android.gms.maps.SupportMapFragment"/> 
      <TextView 
       android:id="@+id/test" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="User Does not have Our App. Tell him/her to Download our app." 
       android:textColor="#000000" 
       android:textSize="22sp" 
       android:background="#60ffffff" 
       android:clickable="true" 
       android:layout_below="@+id/rg_views" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" /> 

     </RelativeLayout> 




     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:background="@android:color/white" 
     android:layout_height="match_parent" > 
     <!-- Editext for Search --> 
     <EditText android:id="@+id/inputSearch" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:hint="Search Contacts.." 
      android:textColor="#000000" 
      android:inputType="textVisiblePassword"/> 
     <ListView 
      android:id="@+id/lst_contacts" 

      android:choiceMode="singleChoice" 
      android:divider="@color/list_divider" 
      android:dividerHeight="1dp" 
      android:listSelector="@drawable/list_selector" 

      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/inputSearch" /> 

    </RelativeLayout> 

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

А вот мой некоторые Java-код для реализации вкладок.

TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); 
    tabHost.setup(); 
    tabHost.setOnTabChangedListener(this); 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
    listViewPhoneBook=(ListView)findViewById(R.id.lst_contacts); 
      // add views to tab host 
      tabHost.addTab(tabHost.newTabSpec("list").setIndicator("List").setContent(new TabHost.TabContentFactory() { 
       public View createTabContent(String arg0) { 
        return listViewPhoneBook; 
       } 
      })); 
      tabHost.addTab(tabHost.newTabSpec("map").setIndicator("Map").setContent(new TabHost.TabContentFactory() { 
       public View createTabContent(String arg0) { 
        return listViewPhoneBook; //if i use mapFragment here its showing error. 
       } 

Ошибка: -

Incompaitable type: 
required: android.view.View 
Found: com.google.android.gms.maps.SupportMapFragment 
+0

Можете ли вы сообщить об ошибке? – droidpl

+0

Вопрос обновлен. обязательно проверьте ошибка в вопросе. – neo

+0

Просьба предоставить полную трассировку стека, она дает гораздо больше информации, чем сообщение – droidpl

ответ

1

Вы должны изменить две вещи:

  • способ создания вкладок, нужно это:

    TabHost.TabSpec spec=tabs.newTabSpec("map"); spec.setContent(R.id.map); spec.setIndicator("Map"); tabs.addTab(spec);

  • Измените RelativeLayout контента на LinearLayout.