我对Android开发真的是一个新手,有几天我会尝试在我的应用程序中添加一个地图。我尝试了一些事情,但没有成功。如果有人能帮我,我很感激。
我做过的事:
06-15 20:31:37.666: E/AndroidRuntime(15687): FATAL EXCEPTION: main 06-15 20:31:37.666: E/AndroidRuntime(15687): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hellomap/com.example.hellomap.MainActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class fragment 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.app.ActivityThread.access$700(ActivityThread.java:134) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.os.Handler.dispatchMessage(Handler.java:99) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.os.Looper.loop(Looper.java:137) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.app.ActivityThread.main(ActivityThread.java:4867) 06-15 20:31:37.666: E/AndroidRuntime(15687): at java.lang.reflect.Method.invokeNative(Native Method) 06-15 20:31:37.666: E/AndroidRuntime(15687): at java.lang.reflect.Method.invoke(Method.java:511) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 06-15 20:31:37.666: E/AndroidRuntime(15687): at dalvik.system.NativeStart.main(Native Method) 06-15 20:31:37.666: E/AndroidRuntime(15687): Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class fragment 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.app.Activity.setContentView(Activity.java:1901) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.example.hellomap.MainActivity.onCreate(MainActivity.java:14) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.app.Activity.performCreate(Activity.java:5047) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056) 06-15 20:31:37.666: E/AndroidRuntime(15687): ... 11 more 06-15 20:31:37.666: E/AndroidRuntime(15687): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4323000 but found 0. You must have the following declaration within the element: 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.common.GooglePlayServicesUtil.t(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.maps.internal.q.B(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.maps.internal.q.A(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.maps.SupportMapFragment$b.gW(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.dynamic.a.a(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.dynamic.a.onInflate(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676) 06-15 20:31:37.666: E/AndroidRuntime(15687): ... 20 more
下面是main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:mapType="normal"/>
这里是清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hellomap"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"/>
<!-- The following four permissions -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:label="@string/app_name"
android:icon="@drawable/ic_launcher">
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDD0f1pf-64LHNlTUWl6a5qeoWqqMw0e3s"/>
<activity
android:name="MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
最后是java
package com.example.hellomap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
试试我的另一个帖子。我已经详细解释了地图的工作。
你现在应该可以使用地图了。
想要使用GoogleMaps-OnMyLocationChangeListener但无法实现?任何其他选项
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="250dp"
class="com.google.android.gms.maps.SupportMapFragment" />
权限如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mike.mylocation"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<permission
android:name="com.mike.maps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.mapsexample.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.mike.mylocation.HomeActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="YOUE API WILL GO HERE" />
</application>
</manifest>
也别忘了使用这些回调,否则你的电池会耗尽的…:p
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
1, this);
}
编辑:
public class MainActivity extends FragmentActivity implements LocationListener,
OnMapClickListener, OnMapLongClickListener {
private static final int GPS_ERRORDIALOG_REQUEST = 9001;
GoogleMap map;
List<Address> matches;
String addressText;
String addressLine;
double latitude;
double longitude;
AppUtils mAppUtils;
Context context;
LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
if (servicesOK()) {
// Toast.makeText(this, "Ready to map!!", Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_main);
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
map = fm.getMap();
// Enabling MyLocation Layer of Google Map
map.setMyLocationEnabled(true);
// Getting LocationManager object from System Service
// LOCATION_SERVICE
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
} else {
setContentView(R.layout.activity_main);
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
//This is the main stuff here. This is when the location is changed.
@Override
public void onLocationChanged(Location location) {
// Getting latitude of the current location
latitude = location.getLatitude();
// Getting longitude of the current location
longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
map.animateCamera(CameraUpdateFactory.zoomTo(12));
map.addMarker(
new MarkerOptions().position(new LatLng(latitude, longitude))
.title("My Location" + "\n")).showInfoWindow();
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
marker.showInfoWindow();
return false;
}
});
//This is optional........................................................
Geocoder geoCoder = new Geocoder(this);
try {
matches = geoCoder.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Address bestMatch = (matches.isEmpty() ? null : matches.get(0));
addressText = String.format("%s, %s, %s", bestMatch
.getMaxAddressLineIndex() > 0 ? bestMatch.getAddressLine(0)
: "", bestMatch.getLocality(), bestMatch.getCountryName());
//...........................................................................
}
@Override
public void onMapLongClick(LatLng point) {
// TODO Auto-generated method stub
}
@Override
public void onMapClick(LatLng point) {
// TODO Auto-generated method stub
}
//Check if the service is OK
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
this, GPS_ERRORDIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(this, "Cant connect!!", Toast.LENGTH_SHORT).show();
}
return false;
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
1, this);
}
我是xamarin的新手,我需要在应用程序中放置谷歌地图。 我已经从xamarin下载了示例: https://github.com/xamarin/monodroid-samples/tree/master/MapsAndLocationDemo_v2 经过一些改变,它工作了,这个项目显示地图。 以下是工作的项目的代码: /MainActivity.cs/ /BasicDemo.axml/ /A
我正在写一个android应用程序,其中包括一个谷歌地图片段。当我将地图片段从包含它的活动中排除时,应用程序运行良好。当我包含映射片段时,我会得到下面blockquote中引用的异常。最让人恼火的是,就在几周前,这还没有发生。这个活动运行了几个星期,然后我更新了android studio,现在我得到了这个例外。我已经在google和stackoverflow上搜索了几个解决方案,但没有任何效果。
activity_main.xml 舱单 错误输出
我有问题加载一个谷歌地图在我的片段。一切都从一个循环器视图适配器开始,该适配器启动活动“ViatGedetAllViewActivity”: 这是我的DetallViewActivity: =2 03-24 02:55:53.881 639-7362/com.example.usuari.MyApplication3 w/ResourcesManager:GetTopleVelResources:
所以我看到了一个特殊的例外,我敢肯定的是谷歌地图的绘图代码。 我有一个片段,在那里我以编程方式添加了一个支持地图片段,然后我在其中操纵谷歌地图实例。 这是stacktrace: 我无法可靠地复制它(尽管这种情况经常发生),我已经查看了ReadWriteDirectByteBuffer和ShortToByteBufferAdapter,但没有任何东西突然出现在我面前。 有什么想法吗?
Google Maps-内存不足异常