谷歌地图2版没有标签内部组件 [英] google maps v2 not working inside tab
问题描述
我创造出谷歌地图的应用程序。它是通过扩展Activity类创建。
但现在我想要显示的onclick标签的地图。这是我的主要活动课。
我创建了3个标签。我想说明在1日选项卡点击地图。
公共类MainActivity扩展FragmentActivity实现ActionBar.TabListener
{
@覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
//动初始化
viewPager =(ViewPager)findViewById(R.id.pager);
动作条= getActionBar();
mAdapter =新TabsPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(假);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); //添加标签
对于(字符串TAB_NAME:标签)
{
actionBar.addTab(actionBar.newTab()的setText(TAB_NAME).setTabListener(本)); } }
下面是地图片段code
公共类MapFragment扩展片段
{
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState)
{
查看rootView = inflater.inflate(R.layout.fragment_map,集装箱,FALSE); 返回rootView; }
}
下面是code为fragment_map.xml
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / RelativeLayout1
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
工具:上下文=MapActivity。><片段
机器人:ID =@ + ID /图
机器人:layout_width =match_parent
机器人:layout_height =200dp
机器人:layout_alignParentLeft =真
机器人:layout_alignParentTop =真
类=com.google.android.gms.maps.MapFragment/><进度
机器人:ID =@ + ID / speedbar
风格=机器人:ATTR / progressBarStyleHorizontal
机器人:layout_width =250dp
机器人:layout_height =50dp
机器人:layout_alignParentBottom =真
机器人:layout_alignParentLeft =真
机器人:layout_below =@ + ID /图
机器人:layout_weight =1/><的EditText
机器人:ID =@ + ID / speed_text
机器人:layout_width =71dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:layout_alignParentRight =真
机器人:layout_alignTop =@ + ID / speedbar
机器人:EMS =10
机器人:启用=假> < requestFocus的/>
< /&的EditText GT;
当我执行上面code它说:
android.view.InflateException:二进制XML文件行#9:错误充气类片段
您的活动应该扩展 Activty
,因为你正在使用 MapFragment
。
你也有 getSupportFragmentManager()
如果您针对API级别11及以下,你应该使用 SupportMapFragment
延长 FragmentActivity
,并使用支持库。
如果不能切换到 MapFragment
。
此外,如果你想显示一个片段里面的地图,你应该使用图形页面
的http://developer.android.com/reference/com/google/android/gms/maps/MapView.html
您可以参考这个
<一个href=\"http://stackoverflow.com/questions/20919048/android-android-view-inflateexception-binary-xml-file-line-8-error-inflatin/20971284#20971284\">Android - android.view.InflateException:二进制XML文件行#8:错误充气类片段
I had created an app showing google map. its was created by extending Activity class. But Now i want the map to be shown onclick of a tab. This is my main activity class. I have created 3 tabs. I want to show map on click of 1st tab.
public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs)
{
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
}
Here is code of map fragment
public class MapFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_map, container, false);
return rootView;
}
}
Here is code for fragment_map.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MapActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
class="com.google.android.gms.maps.MapFragment" />
<ProgressBar
android:id="@+id/speedbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/map"
android:layout_weight="1" />
<EditText
android:id="@+id/speed_text"
android:layout_width="71dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/speedbar"
android:ems="10"
android:enabled="false" >
<requestFocus />
</EditText>
when i execute above code it says
android.view.InflateException: Binary XML file line #9: Error inflating class fragment
Your Activity should extend Activty
since you are using MapFragment
.
Also you have getSupportFragmentManager()
If you targeting api level 11 and below you should use SupportMapFragment
extend FragmentActivity
and use Support Library
If not switch to MapFragment
.
Also if you want to display the map inside a Fragment you should use MapView
http://developer.android.com/reference/com/google/android/gms/maps/MapView.html
You can refer this
Android - android.view.InflateException: Binary XML file line #8: Error inflating class fragment
这篇关于谷歌地图2版没有标签内部组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!