我在运行下面的代码时遇到NullPointerException。我不知道如何解决这些问题。
StackTrace:
E/AndroidRuntime(1118): FATAL EXCEPTION: main
E/AndroidRuntime(1118): Process: info.androidhive.googlemapsv2, PID: 1118
E/AndroidRuntime(1118): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.googlemapsv2/info.androidhive.googlemapsv2.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
E/AndroidRuntime(1118): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
E/AndroidRuntime(1118): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
E/AndroidRuntime(1118): at android.app.ActivityThread.access$800(ActivityThread.java:135)
E/AndroidRuntime(1118): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
E/AndroidRuntime(1118): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(1118): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(1118): at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime(1118): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1118): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(1118): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime(1118): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E/AndroidRuntime(1118): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(1118): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
E/AndroidRuntime(1118): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
E/AndroidRuntime(1118): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
E/AndroidRuntime(1118): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
E/AndroidRuntime(1118): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
E/AndroidRuntime(1118): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
E/AndroidRuntime(1118): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
E/AndroidRuntime(1118): at android.app.Activity.setContentView(Activity.java:1929)
E/AndroidRuntime(1118): at info.androidhive.googlemapsv2.MainActivity.onCreate(MainActivity.java:24)
E/AndroidRuntime(1118): at android.app.Activity.performCreate(Activity.java:5231)
E/AndroidRuntime(1118): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime(1118): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
E/AndroidRuntime(1118): ... 11 more
E/AndroidRuntime(1118): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4452000 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" />
E/AndroidRuntime(1118): at com.google.android.gms.common.GooglePlayServicesUtil.t(Unknown Source)
E/AndroidRuntime(1118): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
E/AndroidRuntime(1118): at com.google.android.gms.maps.internal.u.B(Unknown Source)
E/AndroidRuntime(1118): at com.google.android.gms.maps.internal.u.A(Unknown Source)
E/AndroidRuntime(1118): at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
E/AndroidRuntime(1118): at com.google.android.gms.maps.MapFragment$b.ip(Unknown Source)
E/AndroidRuntime(1118): at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
E/AndroidRuntime(1118): at com.google.android.gms.dynamic.a.a(Unknown Source)
E/AndroidRuntime(1118): at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
E/AndroidRuntime(1118): at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
E/AndroidRuntime(1118): at android.app.Activity.onCreateView(Activity.java:4785)
E/AndroidRuntime(1118): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
E/AndroidRuntime(1118): ... 21 more
MainActivity.java:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends Activity {
// Google Map
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initilizeMap();
// Changing map type
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Showing / hiding your current location
googleMap.setMyLocationEnabled(true);
// Enable / Disable zooming controls
googleMap.getUiSettings().setZoomControlsEnabled(false);
// Enable / Disable my location button
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
// Enable / Disable Compass icon
googleMap.getUiSettings().setCompassEnabled(true);
// Enable / Disable Rotate gesture
googleMap.getUiSettings().setRotateGesturesEnabled(true);
// Enable / Disable zooming functionality
googleMap.getUiSettings().setZoomGesturesEnabled(true);
double latitude = 17.385044;
double longitude = 78.486671;
// lets place some 10 random markers
for (int i = 0; i < 10; i++) {
// random latitude and logitude
double[] randomLocation = createRandLocation(latitude,
longitude);
// Adding a marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(randomLocation[0], randomLocation[1]))
.title("Hello Maps " + i);
Log.e("Random", "> " + randomLocation[0] + ", "
+ randomLocation[1]);
// changing marker color
if (i == 0)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
if (i == 1)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
if (i == 2)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_CYAN));
if (i == 3)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
if (i == 4)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
if (i == 5)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
if (i == 6)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED));
if (i == 7)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
if (i == 8)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
if (i == 9)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
googleMap.addMarker(marker);
// Move the camera to last position with a zoom level
if (i == 9) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(randomLocation[0],
randomLocation[1])).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
/**
* function to load map If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
/*
* creating random postion around a location for testing purpose only
*/
private double[] createRandLocation(double latitude, double longitude) {
return new double[] { latitude + ((Math.random() - 0.5) / 500),
longitude + ((Math.random() - 0.5) / 500),
150 + ((Math.random() - 0.5) * 10) };
}
}
Maifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.androidhive.googlemapsv2"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<!-- Requires OpenGL ES version 2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name="info.androidhive.googlemapsv2.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppBaseTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Goolge API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyB2p7rsFLCuYmjA8SnRsIZfjWugwgh4wtU" />
</application>
</manifest>
我收到了NullPointerException。我不知道如何摆脱错误。任何人都知道如何解决这些问题。谢谢。
答案 0 :(得分:11)
你的logcat清楚地说了
Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4452000 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" />
E/AndroidRuntime(1118): at com.google.android.gms.common.GooglePlayServicesUtil.t(Unknown Source)
E/AndroidRuntime(1118): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
E/AndroidRuntime(1118): at com.google.android.gms.maps.internal.u.B(Unknown Source)
E/AndroidRuntime(1118): at com.google.android.gms.maps.internal.u.A(Unkno
您需要在<meta-data>
<application>
标记下添加Google Play服务版本manifest.xml
标记
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />