gpt4 book ai didi

android - 添加 "mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();"后应用程序不断崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 12:58:44 27 4
gpt4 key购买 nike

我正在尝试学习如何使用 Google API V2 开发谷歌地图 Android 应用程序。当我没有添加

时它起作用
private GoogleMap mMap = null;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

MyGoogleMapDemo.java 中但是当我添加它们时,应用程序一直在崩溃。我正在使用

  • 谷歌 API 4.2
  • Android SDK 工具 21.1
  • Android SDK 平台-工具 16.0.1
  • Eclipse SDK 3.7.1

而且我还将 android-support-v4.jar 添加到我的项目中。我很期待

MyGoogleMapDemo.java

package org.lxh.demo;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MyGoogleMapDemo extends FragmentActivity
{

private GoogleMap mMap = null;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

}

ma​​in.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.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"

map:cameraBearing="112.5"
map:cameraTargetLat="-33.796923"
map:cameraTargetLng="150.922433"
map:cameraTilt="30"
map:cameraZoom="13"
map:mapType="normal"
map:uiCompass="true"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiTiltGestures="true"
map:uiZoomControls="false"
map:uiZoomGestures="true" />

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.lxh.demo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />

<permission
android:name="org.lxh.demo.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />

<uses-permission android:name="org.lxh.demo.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" />

<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCHCxLLeMk3zRL4TYICdbYjB6nQk-oCvRs" />

<uses-library android:name="com.google.android.maps" />

<activity
android:name=".MyGoogleMapDemo"
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>

日志.txt

02-17 22:56:51.954: D/ActivityThread(11229): setTargetHeapUtilization:0.25
02-17 22:56:51.954: D/ActivityThread(11229): setTargetHeapIdealFree:8388608
02-17 22:56:51.954: D/ActivityThread(11229): setTargetHeapConcurrentStart:2097152
02-17 22:56:52.585: W/dalvikvm(11229): threadid=1: thread exiting with uncaught exception (group=0x4106d498)
02-17 22:56:52.585: E/AndroidRuntime(11229): FATAL EXCEPTION: main
02-17 22:56:52.585: E/AndroidRuntime(11229): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.lxh.demo/org.lxh.demo.MyGoogleMapDemo}: java.lang.NullPointerException
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2080)
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2105)
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.app.ActivityThread.access$600(ActivityThread.java:136)
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.os.Looper.loop(Looper.java:137)
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.app.ActivityThread.main(ActivityThread.java:4797)
02-17 22:56:52.585: E/AndroidRuntime(11229): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 22:56:52.585: E/AndroidRuntime(11229): at java.lang.reflect.Method.invoke(Method.java:511)
02-17 22:56:52.585: E/AndroidRuntime(11229): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
02-17 22:56:52.585: E/AndroidRuntime(11229): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
02-17 22:56:52.585: E/AndroidRuntime(11229): at dalvik.system.NativeStart.main(Native Method)
02-17 22:56:52.585: E/AndroidRuntime(11229): Caused by: java.lang.NullPointerException
02-17 22:56:52.585: E/AndroidRuntime(11229): at org.lxh.demo.MyGoogleMapDemo.onCreate(MyGoogleMapDemo.java:24)
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.app.Activity.performCreate(Activity.java:5024)
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2044)
02-17 22:56:52.585: E/AndroidRuntime(11229): ... 11 more

最佳答案

您的 LogCat 中可能还有与您的问题相关的其他消息。我的猜测是你的问题是你有一个格式错误的 <fragment>元素。你有:

<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.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
... />

你不应该两者 android:name class , 他们当然不应该指向不同的类。 android:name是首选方法。由于您继承自 FragmentActivity ,我猜你真的想要 android:name指向SupportMapFragment , 所以进行更改并删除 class属性。您可能还想摆脱 xmlns:map和所有 map:属性,直到您使其余代码正常工作。

关于android - 添加 "mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();"后应用程序不断崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14922678/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com