我正在尝试制作GoogleMap应用程序。但是当我开始它时,我有一个例外。这是我的代码和LogCat输出。当我启动应用程序时,“目标”列中有一个感叹号。非常感谢!!!
这是我的主要课程:
package com.example.googlem;
import java.util.ArrayList;
import java.util.List;
import android.app.Notification.Style;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.Projection;
import com.google.android.maps.*;
public class MainActivity extends FragmentActivity implements LocationListener {
LocationManager locman;
LocationListener loclis;
Location Location;
private MapView map;
List<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();
private MapController controller;
String provider = LocationManager.GPS_PROVIDER;
double lat;
double lon;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initMapView();
initMyLocation();
locman = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// locman.requestLocationUpdates(provider,60000, 100,loclis);
// Location = locman.getLastKnownLocation(provider);
}
/** Find and initialize the map view. */
private void initMapView() {
//map = ((SupportMapFragment) getSupportFragmentManager()
//.findFragmentById(R.id.map)).getMap();
map = (MapView) findViewById(R.id.map);
controller = map.getController();
map.setSatellite(false);
map.setBuiltInZoomControls(true);
}
/** Find Current Position on Map. */
private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
overlay.enableMyLocation();
overlay.enableCompass();
overlay.runOnFirstFix(new Runnable() {
public void run() {
// Zoom in to current location
controller.setZoom(24);
controller.animateTo(overlay.getMyLocation());
}
});
map.getOverlays().add(overlay);
}
@Override
public void onLocationChanged(Location location) {
if (Location != null) {
lat = Location.getLatitude();
lon = Location.getLongitude();
GeoPoint New_geopoint = new GeoPoint((int) (lat * 1e6),
(int) (lon * 1e6));
controller.animateTo(New_geopoint);
}
}
class MyOverlay extends Overlay {
public MyOverlay() {
}
public void draw(Canvas canvas, MapView mapv, boolean shadow) {
super.draw(canvas, mapv, shadow);
com.google.android.maps.Projection projection = map.getProjection();
Path p = new Path();
for (int i = 0; i < geoPointsArray.size(); i++) {
if (i == geoPointsArray.size() - 1) {
break;
}
Point from = new Point();
Point to = new Point();
projection.toPixels(geoPointsArray.get(i), from);
projection.toPixels(geoPointsArray.get(i + 1), to);
p.moveTo(from.x, from.y);
p.lineTo(to.x, to.y);
}
Paint mPaint = new Paint();
// mPaint.setStyle(Style.STROKE);
mPaint.setColor(0xFFFF0000);
mPaint.setAntiAlias(true);
canvas.drawPath(p, mPaint);
super.draw(canvas, map, shadow);
}
}
@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
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="403dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlem"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<permission android:name="com.example.googlem.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="com.example.googlem.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<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" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps"/>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCQ9zcNrIq6ooXc5GeACEFdaHARxB9GnVU"/>
<activity
android:name="com.example.googlem.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>
LogCat输出:
11-30 09:14:47.323: E/AndroidRuntime(5297): FATAL EXCEPTION: main
11-30 09:14:47.323: E/AndroidRuntime(5297): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlem/com.example.googlem.MainActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class fragment
11-30 09:14:47.323: E/AndroidRuntime(5297): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463)
11-30 09:14:47.323: E/AndroidRuntime(5297): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
11-30 09:14:47.323: E/AndroidRuntime(5297): at android.app.ActivityThread.access$600(ActivityThread.java:162)
11-30 09:14:47.323: E/AndroidRuntime(5297): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
11-30 09:14:47.323: E/AndroidRuntime(5297): at android.os.Handler.dispatchMessage(Handler.java:99)
11-30 09:14:47.323: E/AndroidRuntime(5297): at android.os.Looper.loop(Looper.java:158)
11-30 09:14:47.323: E/AndroidRuntime(5297): at android.app.ActivityThread.main(ActivityThread.java:5751)
11-30 09:14:47.323: E/AndroidRuntime(5297): at java.lang.reflect.Method.invokeNative(Native Method)
11-30 09:14:47.323: E/AndroidRuntime(5297): at java.lang.reflect.Method.invoke(Method.java:511)
11-30 09:14:47.323: E/AndroidRuntime(5297): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
11-30 09:14:47.323: E/AndroidRuntime(5297): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
11-30 09:14:47.323: E/AndroidRuntime(5297): at dalvik.system.NativeStart.main(Native Method)
11-30 09:14:47.323: E/AndroidRuntime(5297): Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class fragment
11-30 09:14:47.323: E/AndroidRuntime(5297): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
11-30 09:14:47.323: E/AndroidRuntime(5297): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-30 09:14:47.323: E/AndroidRuntime(5297): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-30 09:14:47.323: E/AndroidRuntime(5297): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-30 09:14:47.323: E/AndroidRuntime(5297): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-30 09:14:47.323: E/AndroidRuntime(5297): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:342)
11-30 09:14:47.323: E/AndroidRuntime(5297): at android.app.Activity.setContentView(Activity.java:1929)
11-30 09:14:47.323: E/AndroidRuntime(5297): at com.example.googlem.MainActivity.onCreate(MainActivity.java:87)
11-30 09:14:47.323: E/AndroidRuntime(5297): at android.app.Activity.performCreate(Activity.java:5165)
11-30 09:14:47.323: E/AndroidRuntime(5297): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
11-30 09:14:47.323: E/AndroidRuntime(5297): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
11-30 09:14:47.323: E/AndroidRuntime(5297): ... 11 more
11-30 09:14:47.323: E/AndroidRuntime(5297): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4030500 but found 0. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
11-30 09:14:47.323: E/AndroidRuntime(5297): at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
11-30 09:14:47.323: E/AndroidRuntime(5297): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
11-30 09:14:47.323: E/AndroidRuntime(5297): at com.google.android.gms.maps.internal.q.v(Unknown Source)
11-30 09:14:47.323: E/AndroidRuntime(5297): at com.google.android.gms.maps.internal.q.u(Unknown Source)
11-30 09:14:47.323: E/AndroidRuntime(5297): at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
11-30 09:14:47.323: E/AndroidRuntime(5297): at com.google.android.gms.maps.SupportMapFragment$b.cE(Unknown Source)
11-30 09:14:47.323: E/AndroidRuntime(5297): at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source)
11-30 09:14:47.323: E/AndroidRuntime(5297): at com.google.android.gms.dynamic.a.a(Unknown Source)
11-30 09:14:47.323: E/AndroidRuntime(5297): at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
11-30 09:14:47.323: E/AndroidRuntime(5297): at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
11-30 09:14:47.323: E/AndroidRuntime(5297): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290)
11-30 09:14:47.323: E/AndroidRuntime(5297): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
11-30 09:14:47.323: E/AndroidRuntime(5297): ... 21 more
答案 0 :(得分:0)
请参阅您的例外说明:
您必须在清单的应用标记中包含以下声明:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />