1
我在關注如何使Google Maps API v2正常工作。它顯示了一個地圖片段,但第二個我在一個活動中調用了getmap(),該應用程序崩潰時發生了NullPointerException異常。Google Maps API v2調用getmap()拋出NullPointerException(已解決)
這裏是我的RunProgress.java:
package dk.aau.student.runapp;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class RunProgress extends Activity {
// Google Map
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_run_progress);
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initializeMap() {
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();
}
}
}
@Override
protected void onResume() {
super.onResume();
initializeMap();
}
}
這裏是layout.xml:
<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="dk.aau.student.runapp.RunProgress$MapFrag">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>
這裏是清單:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dk.aau.student.runapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19"
/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.vending.CHECK_LICENSE" />
<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" />
<!--
The following two permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<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:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- GOOGLE API KEY -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="API_KEY" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="dk.aau.student.runapp.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>
<activity
android:name="dk.aau.student.runapp.RunOptions"
android:label="@string/title_activity_run_options"
android:parentActivityName="dk.aau.student.runapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="MainActivity" />
</activity>
<activity
android:name="dk.aau.student.runapp.MatchComp"
android:label="@string/title_activity_matchmake"
android:parentActivityName="dk.aau.student.runapp.RunOptions" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="RunOptions" />
</activity>
<activity
android:name="dk.aau.student.runapp.MatchFriend"
android:label="@string/title_activity_matchmake"
android:parentActivityName="dk.aau.student.runapp.RunOptions" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="RunOptions" />
</activity>
<activity
android:name="dk.aau.student.runapp.PickRoute"
android:label="@string/title_activity_pick_route"
android:parentActivityName="dk.aau.student.runapp.MatchComp" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="MatchComp" />
</activity>
<activity
android:name="dk.aau.student.runapp.RunProgress"
android:label="@string/title_activity_run_progress" >
</activity>
</application>
</manifest>
注:我有一個API鍵,我剛剛從這個清單中刪除它。
這裏是我的logcat:
03-28 17:01:48.222: E/AndroidRuntime(12800): FATAL EXCEPTION: main
03-28 17:01:48.222: E/AndroidRuntime(12800): java.lang.RuntimeException: Unable to resume activity {dk.aau.student.runapp/dk.aau.student.runapp.RunProgress}: java.lang.NullPointerException
03-28 17:01:48.222: E/AndroidRuntime(12800): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
03-28 17:01:48.222: E/AndroidRuntime(12800): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2760)
03-28 17:01:48.222: E/AndroidRuntime(12800): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2216)
03-28 17:01:48.222: E/AndroidRuntime(12800): at android.app.ActivityThread.access$600(ActivityThread.java:149)
03-28 17:01:48.222: E/AndroidRuntime(12800): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
03-28 17:01:48.222: E/AndroidRuntime(12800): at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 17:01:48.222: E/AndroidRuntime(12800): at android.os.Looper.loop(Looper.java:153)
03-28 17:01:48.222: E/AndroidRuntime(12800): at android.app.ActivityThread.main(ActivityThread.java:4987)
03-28 17:01:48.222: E/AndroidRuntime(12800): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 17:01:48.222: E/AndroidRuntime(12800): at java.lang.reflect.Method.invoke(Method.java:511)
03-28 17:01:48.222: E/AndroidRuntime(12800): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
03-28 17:01:48.222: E/AndroidRuntime(12800): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
03-28 17:01:48.222: E/AndroidRuntime(12800): at dalvik.system.NativeStart.main(Native Method)
03-28 17:01:48.222: E/AndroidRuntime(12800): Caused by: java.lang.NullPointerException
03-28 17:01:48.222: E/AndroidRuntime(12800): at dk.aau.student.runapp.RunProgress.initializeMap(RunProgress.java:28)
03-28 17:01:48.222: E/AndroidRuntime(12800): at dk.aau.student.runapp.RunProgress.onResume(RunProgress.java:42)
03-28 17:01:48.222: E/AndroidRuntime(12800): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1189)
03-28 17:01:48.222: E/AndroidRuntime(12800): at android.app.Activity.performResume(Activity.java:5094)
03-28 17:01:48.222: E/AndroidRuntime(12800): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2718)
03-28 17:01:48.222: E/AndroidRuntime(12800): ... 12 more
我老老實實在我束手無策這裏。
不知道,但你可能想刪除你的工具:上下文屬性設置在你的佈局。 – zgc7009