我正在关注this tutorial以使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
我老实说,我的智慧结束了。
答案 0 :(得分:4)
我正在解决您的解决方案,并将其放入正确的答案以协助将来的搜索。
发现问题:在我尝试调用getmap()之前没有创建片段,因为名为onCreate()的布局是activity_run_progress布局,而不是fragment_run_progress。我已经将contentView设置为片段布局并删除了Fragment类,因为我现在不需要它。运行活动中的所有内容。
使用工作代码,我的课程现在看起来像这样:
public class RunProgress extends Activity
{
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_run_progress);
initializeMap();
}
/**
* 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();
}
}
}
}
我的布局如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
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"
tools:ignore="MergeRootFrame">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>