在这个程序,我想添加一个功能V2谷歌地图。我看大部分的教程创建活动,取得了关键的地图,并创建XML文件来试试,看看我是否错过了任何一个步骤(我走过他们像4倍),并且它不看起来像我错过了任何一个步骤。
这里是我用过的网站:
当我在我的手机上运行我的应用程序(因为谷歌播放服务并没有在模拟器上工作),它给了我两个错误:
02-11 22:10:54.690:E / AndroidRuntime(4526):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.example.myfirstapp / com.example。 myfirstapp.ShowActivity}:android.view.InflateException:二进制XML文件中的行#8:错误充气类片段
02-11 22:10:54.690:E / AndroidRuntime(4526):
android.app.Fragment $ InstantiationException:产生的原因无法实例片段com.google.android.gms.maps.MapFragment:确保类名称存在,是公开的,并且有一个空的构造是公
下面是我的活动:
包com.example.myfirstapp;
进口android.app.Activity;
进口android.os.Bundle;
进口android.support.v4.app.FragmentActivity;
进口android.view.Menu;
进口com.google.android.maps *。
进口com.google.android.gms.maps.MapFragment;
公共类ShowActivity延伸活动{
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_map);
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.activity_map,菜单);
返回true;
}
// @覆盖
//保护布尔isRouteDisplayed(){
// TODO自动生成方法存根
//返回false;
//}
}
抱歉的格式,但这里是我的xml文件:
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=。ShowActivity>
<片段
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /图
机器人:layout_width =match_parent
机器人:layout_height =match_parent
类=com.google.android.gms.maps.MapFragment/>
< / RelativeLayout的>
我已经进口了谷歌API和谷歌播放,因此所有的进口工作,这使我想知道为什么第二错误说MapFragment是空的服务。 清单文件code:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.myfirstapp
安卓版code =1
机器人:VERSIONNAME =1.0>
<使用-权限的Android:名称=android.permission.READ_CONTACTS/>
<使用-权限的Android:名称=android.permission.SUBSCRIBED_FEEDS_READ/>
<使用-权限的Android:名称=android.permission.ACCESS_FINE_LOCATION/>
<使用-权限的Android:名称=android.permission.ACCESS_COARSE_LOCATION/>
<使用-权限的Android:名称=com.google.android.providers.gsf.permission.READ_GSERVICES/>
<使用-权限的Android:名称=android.permission.INTERNET对/>
<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
<使用-权限的Android:名称=com.example.myfirstapp.permission.MAPS_RECEIVE/>
<许可
机器人:名称=com.example.myfirstapp.permission.MAPS_RECEIVE
安卓的ProtectionLevel =签名/>
<用途特征
机器人:glEsVersion =0x00020000
机器人:要求=真/>
<用途-SDK
安卓的minSdkVersion =14
机器人:targetSdkVersion =15/>
<应用
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名称=com.example.myfirstapp.MainActivity
机器人:标签=@字符串/ title_activity_main>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
&所述;元数据
机器人:名称=android.app.default_searchable
SearchResultsActivity:机器人值= />
< /活性GT;
<活动
机器人:名称=com.example.myfirstapp.SearchResultsActivity
机器人:标签=@字符串/ title_activity_search_results
机器人:launchMode =singleTop>
<意向滤光器>
<作用机器人:名称=android.intent.action.SEARCH/>
&所述; /意图滤光器>
&所述;元数据
机器人:名称=android.app.searchable
机器人:资源=@ XML /搜索/>
< /活性GT;
<活动
机器人:名称=com.example.myfirstapp.AddContacts
机器人:标签=@字符串/ title_activity_add_contacts>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动
机器人:名称=com.example.myfirstapp.ContactActivity
机器人:标签=@字符串/ title_activity_contact>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动
机器人:名称=com.example.myfirstapp.DisplayInfo
机器人:标签=@字符串/ title_activity_display_info>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<供应商
机器人:名称=com.example.myfirstapp.SomeProvider
机器人:当局=com.example.myfirstapp.SomeProvider>
< /供应商>
<活动
机器人:名称=com.example.myfirstapp.EditContacts
机器人:标签=@字符串/ title_activity_edit_contacts>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动
机器人:名称=com.example.myfirstapp.DeleteContact
机器人:标签=@字符串/ title_activity_delete_contact>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动
机器人:名称=com.example.myfirstapp.FlightInfo
机器人:标签=@字符串/ title_activity_flight_info>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动
机器人:名称=com.example.myfirstapp.EventInfo
机器人:标签=@字符串/ title_activity_event_info>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动
机器人:名称=com.example.myfirstapp.ContactsActivity
机器人:标签=@字符串/ title_activity_contacts>
< /活性GT;
<活动
机器人:名称=com.example.myfirstapp.ShowActivity
机器人:标签=@字符串/ title_activity_map>
< /活性GT;
&所述;元数据
机器人:名称=com.google.android.maps.v2.API_KEY
机器人:值=删除/>
<使用库机器人:名称=com.google.android.maps/>
< /用途>
< /舱单>
解决方案
试用低于code:
公共类ShowActivity扩展android.support.v4.app.FragmentActivity {
私人GoogleMap的MMAP;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_map);
setUpMapIfNeeded();
}
@覆盖
保护无效onResume(){
super.onResume();
setUpMapIfNeeded();
}
私人无效setUpMapIfNeeded(){
//做一个空检查确认,我们还没有实例化的地图。
如果(MMAP == NULL){
//尝试获取来自SupportMapFragment地图。
MMAP =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map))
.getMap();
//检查如果我们成功取得地图。
如果(MMAP!= NULL){
setUpMap();
}
}
}
XML文件code:
<片段的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /图
机器人:layout_width =match_parent
机器人:layout_height =match_parent
类=com.google.android.gms.maps.SupportMapFragment/>
清单文件code:
<许可
机器人:名称=com.example.myfirstapp.permission.MAPS_RECEIVE
安卓的ProtectionLevel =签名/>
<使用-权限的Android:名称=com.example.myfirstapp.permission.MAPS_RECEIVE/>
<! - 从谷歌地图图书馆/ AndroidManifest.xml中复制。 - >
<用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =16/>
<使用-权限的Android:名称=android.permission.INTERNET对/>
<使用-权限的Android:名称=com.google.android.providers.gsf.permission.READ_GSERVICES/>
<! - 外部存储缓存。 - >
<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
<用途特征
机器人:glEsVersion =0x00020000
机器人:要求=真/>
<应用
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
&所述;元数据
机器人:名称=com.google.android.maps.v2.API_KEY
机器人:值=删除/>
< /用途>
In this app, I am trying to add a functional V2 google map. I've looked at most of the tutorials for creating an activity, obtaining a key for the maps, and creating the xml file to try and see if I missed any step (I went through them like 4 times), and it doesn't seem like I missed any step.
Here is the site I used:
When I run my app on my phone(since Google play services doesn't work on the emulator), it gives me these two errors:
02-11 22:10:54.690: E/AndroidRuntime(4526): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.ShowActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
02-11 22:10:54.690: E/AndroidRuntime(4526):
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
Here is my activity:
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import com.google.android.maps.*;
import com.google.android.gms.maps.MapFragment;
public class ShowActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_map, menu);
return true;
}
//@Override
//protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
//return false;
//}
}
Sorry for the formatting but here is my xml file:
<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"
tools:context=".ShowActivity" >
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
I have imported the Google API and google play services so all the imports are working, which makes me wonder why the 2nd error said MapFragment was empty. Manifest file code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SUBSCRIBED_FEEDS_READ" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.example.myfirstapp.permission.MAPS_RECEIVE" />
<permission
android:name="com.example.myfirstapp.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myfirstapp.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
</activity>
<activity
android:name="com.example.myfirstapp.SearchResultsActivity"
android:label="@string/title_activity_search_results"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity
android:name="com.example.myfirstapp.AddContacts"
android:label="@string/title_activity_add_contacts" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myfirstapp.ContactActivity"
android:label="@string/title_activity_contact" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myfirstapp.DisplayInfo"
android:label="@string/title_activity_display_info" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="com.example.myfirstapp.SomeProvider"
android:authorities="com.example.myfirstapp.SomeProvider" >
</provider>
<activity
android:name="com.example.myfirstapp.EditContacts"
android:label="@string/title_activity_edit_contacts" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myfirstapp.DeleteContact"
android:label="@string/title_activity_delete_contact" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myfirstapp.FlightInfo"
android:label="@string/title_activity_flight_info" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myfirstapp.EventInfo"
android:label="@string/title_activity_event_info" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myfirstapp.ContactsActivity"
android:label="@string/title_activity_contacts" >
</activity>
<activity
android:name="com.example.myfirstapp.ShowActivity"
android:label="@string/title_activity_map" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="removed" />
<uses-library android:name="com.google.android.maps" />
</application>
</manifest>
解决方案
Try out below code:
public class ShowActivity extends android.support.v4.app.FragmentActivity {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
XML file code:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
Manifest file code:
<permission
android:name="com.example.myfirstapp.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.myfirstapp.permission.MAPS_RECEIVE"/>
<!-- Copied from Google Maps Library/AndroidManifest.xml. -->
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- External storage for caching. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="removed" />
</application>