我正在尝试在我的Android应用中实现Google地图。以下是我的代码:
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.connect);
initilizeMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(false);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
}
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();
}
}
和我的xml文件:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Logcat错误:
07-24 07:27:44.636: E/SoundPool(382): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:27:44.636: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-24 07:27:44.646: E/SoundPool(382): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:27:44.646: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-24 07:27:44.646: E/SoundPool(382): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:27:44.646: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-24 07:27:44.646: E/SoundPool(382): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:27:44.646: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-24 07:27:44.646: E/SoundPool(382): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:27:44.646: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-24 07:27:44.656: E/SoundPool(382): error loading /system/media/audio/ui/KeypressStandard.ogg
07-24 07:27:44.656: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
07-24 07:27:44.676: E/SoundPool(382): error loading /system/media/audio/ui/KeypressSpacebar.ogg
07-24 07:27:44.676: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
07-24 07:27:44.676: E/SoundPool(382): error loading /system/media/audio/ui/KeypressDelete.ogg
07-24 07:27:44.686: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
07-24 07:27:44.686: E/SoundPool(382): error loading /system/media/audio/ui/KeypressReturn.ogg
07-24 07:27:44.686: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
07-24 07:27:44.686: E/SoundPool(382): error loading /system/media/audio/ui/KeypressInvalid.ogg
07-24 07:27:44.686: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
07-24 07:27:44.686: W/AudioService(382): onLoadSoundEffects(), Error -1 while loading samples
07-24 07:27:46.426: W/Response(1358): {"GET":[],"POST":{"action":"login","app_secret":"jkhljkUILJGJkljhkjUGLG87796587687HGKJhghkjKUYGKJHjhgjUYGKUY7865876hgKUYGK","email":"a","password":"aa"},"status":"ERROR","msg":"Sorry ! Invalid Username or Password"}
07-24 07:27:46.526: I/ActivityManager(382): START u0 {cmp=example.atlcitylimo/.Home} from pid 1358
07-24 07:27:46.686: I/Choreographer(1358): Skipped 31 frames! The application may be doing too much work on its main thread.
07-24 07:27:46.966: E/dalvikvm(1358): Could not find class 'com.google.android.gms.maps.MapFragment', referenced from method example.atlcitylimo.Home.initilizeMap
07-24 07:27:46.976: W/dalvikvm(1358): VFY: unable to resolve check-cast 992 (Lcom/google/android/gms/maps/MapFragment;) in Lexample/atlcitylimo/Home;
07-24 07:27:46.976: D/dalvikvm(1358): VFY: replacing opcode 0x1f at 0x000f
07-24 07:27:47.036: I/dalvikvm(1358): Could not find method com.google.android.gms.maps.GoogleMap.setMapType, referenced from method example.atlcitylimo.Home.onCreate
07-24 07:27:47.036: W/dalvikvm(1358): VFY: unable to resolve virtual method 8258: Lcom/google/android/gms/maps/GoogleMap;.setMapType (I)V
07-24 07:27:47.086: D/dalvikvm(1358): VFY: replacing opcode 0x6e at 0x0042
07-24 07:27:47.386: D/dalvikvm(1358): GC_FOR_ALLOC freed 235K, 6% free 5764K/6080K, paused 86ms, total 88ms
07-24 07:27:47.386: I/dalvikvm-heap(1358): Grow heap (frag case) to 8.048MB for 2457616-byte allocation
07-24 07:27:47.526: D/dalvikvm(1358): GC_FOR_ALLOC freed 1K, 4% free 8162K/8484K, paused 138ms, total 138ms
07-24 07:27:48.956: D/AndroidRuntime(1358): Shutting down VM
07-24 07:27:48.956: W/dalvikvm(1358): threadid=1: thread exiting with uncaught exception (group=0xb3a75ba8)
07-24 07:27:49.056: E/AndroidRuntime(1358): FATAL EXCEPTION: main
07-24 07:27:49.056: E/AndroidRuntime(1358): Process: example.atlcitylimo, PID: 1358
07-24 07:27:49.056: E/AndroidRuntime(1358): java.lang.RuntimeException: Unable to start activity ComponentInfo{example.atlcitylimo/example.atlcitylimo.Home}: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.os.Handler.dispatchMessage(Handler.java:102)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.os.Looper.loop(Looper.java:136)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-24 07:27:49.056: E/AndroidRuntime(1358): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 07:27:49.056: E/AndroidRuntime(1358): at java.lang.reflect.Method.invoke(Method.java:515)
07-24 07:27:49.056: E/AndroidRuntime(1358): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-24 07:27:49.056: E/AndroidRuntime(1358): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-24 07:27:49.056: E/AndroidRuntime(1358): at dalvik.system.NativeStart.main(Native Method)
07-24 07:27:49.056: E/AndroidRuntime(1358): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
07-24 07:27:49.056: E/AndroidRuntime(1358): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.app.Activity.setContentView(Activity.java:1929)
07-24 07:27:49.056: E/AndroidRuntime(1358): at example.atlcitylimo.Home.onCreate(Home.java:45)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.app.Activity.performCreate(Activity.java:5231)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-24 07:27:49.056: E/AndroidRuntime(1358): ... 11 more
07-24 07:27:49.056: E/AndroidRuntime(1358): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.app.Fragment.instantiate(Fragment.java:597)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.app.Fragment.instantiate(Fragment.java:561)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.app.Activity.onCreateView(Activity.java:4778)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
07-24 07:27:49.056: E/AndroidRuntime(1358): ... 22 more
07-24 07:27:49.056: E/AndroidRuntime(1358): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: DexPathList[[zip file "/data/app/example.atlcitylimo-1.apk"],nativeLibraryDirectories=[/data/app-lib/example.atlcitylimo-1, /vendor/lib, /system/lib]]
07-24 07:27:49.056: E/AndroidRuntime(1358): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
07-24 07:27:49.056: E/AndroidRuntime(1358): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
07-24 07:27:49.056: E/AndroidRuntime(1358): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
07-24 07:27:49.056: E/AndroidRuntime(1358): at android.app.Fragment.instantiate(Fragment.java:583)
07-24 07:27:49.056: E/AndroidRuntime(1358): ... 25 more
07-24 07:27:49.476: W/ActivityManager(382): Force finishing activity example.atlcitylimo/.Home
07-24 07:27:49.526: W/ActivityManager(382): Force finishing activity example.atlcitylimo/.Main
07-24 07:27:49.796: D/dalvikvm(382): GC_FOR_ALLOC freed 517K, 20% free 6664K/8264K, paused 132ms, total 135ms
07-24 07:27:49.796: I/dalvikvm-heap(382): Grow heap (frag case) to 7.196MB for 635812-byte allocation
07-24 07:27:49.926: D/dalvikvm(382): GC_FOR_ALLOC freed 3K, 19% free 7282K/8888K, paused 134ms, total 134ms
07-24 07:27:49.936: I/Choreographer(382): Skipped 44 frames! The application may be doing too much work on its main thread.
07-24 07:27:50.806: I/Choreographer(382): Skipped 71 frames! The application may be doing too much work on its main thread.
07-24 07:27:50.976: W/ActivityManager(382): Activity pause timeout for ActivityRecord{b3fc3d40 u0 example.atlcitylimo/.Home t2 f}
07-24 07:27:51.386: I/Choreographer(382): Skipped 151 frames! The application may be doing too much work on its main thread.
07-24 07:27:51.496: I/Choreographer(554): Skipped 96 frames! The application may be doing too much work on its main thread.
07-24 07:27:51.516: I/Choreographer(382): Skipped 35 frames! The application may be doing too much work on its main thread.
07-24 07:27:51.946: I/Choreographer(382): Skipped 119 frames! The application may be doing too much work on its main thread.
07-24 07:27:52.406: D/dalvikvm(554): GC_FOR_ALLOC freed 445K, 16% free 5555K/6564K, paused 65ms, total 70ms
07-24 07:27:52.836: I/Choreographer(382): Skipped 38 frames! The application may be doing too much work on its main thread.
07-24 07:27:53.096: I/Choreographer(382): Skipped 41 frames! The application may be doing too much work on its main thread.
07-24 07:27:53.196: I/ActivityManager(382): Killing 860:com.android.music/u0a35 (adj 15): empty #17
07-24 07:27:53.466: I/Choreographer(382): Skipped 33 frames! The application may be doing too much work on its main thread.
07-24 07:27:53.486: W/MediaFocusControl(382): RemoteControlClient died
07-24 07:28:03.166: W/ActivityManager(382): Activity destroy timeout for ActivityRecord{b40820e0 u0 example.atlcitylimo/.Main t2 f}
07-24 07:28:03.216: W/ActivityManager(382): Activity destroy timeout for ActivityRecord{b3fc3d40 u0 example.atlcitylimo/.Home t2 f}
07-24 07:28:04.626: I/Process(1358): Sending signal. PID: 1358 SIG: 9
07-24 07:28:04.646: E/SoundPool(382): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:28:04.646: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-24 07:28:04.656: E/SoundPool(382): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:28:04.656: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-24 07:28:04.656: E/SoundPool(382): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:28:04.656: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-24 07:28:04.656: E/SoundPool(382): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:28:04.656: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-24 07:28:04.676: E/SoundPool(382): error loading /system/media/audio/ui/Effect_Tick.ogg
07-24 07:28:04.676: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
07-24 07:28:04.726: E/SoundPool(382): error loading /system/media/audio/ui/KeypressStandard.ogg
07-24 07:28:04.726: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
07-24 07:28:04.776: E/SoundPool(382): error loading /system/media/audio/ui/KeypressSpacebar.ogg
07-24 07:28:04.776: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
07-24 07:28:04.776: E/SoundPool(382): error loading /system/media/audio/ui/KeypressDelete.ogg
07-24 07:28:04.776: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
07-24 07:28:04.786: E/SoundPool(382): error loading /system/media/audio/ui/KeypressReturn.ogg
07-24 07:28:04.786: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
07-24 07:28:04.786: E/SoundPool(382): error loading /system/media/audio/ui/KeypressInvalid.ogg
07-24 07:28:04.786: W/AudioService(382): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
07-24 07:28:04.786: W/AudioService(382): onLoadSoundEffects(), Error -1 while loading samples
07-24 07:28:04.826: I/Choreographer(382): Skipped 52 frames! The application may be doing too much work on its main thread.
07-24 07:28:05.106: I/Choreographer(382): Skipped 31 frames! The application may be doing too much work on its main thread.
07-24 07:28:05.236: I/Choreographer(382): Skipped 30 frames! The application may be doing too much work on its main thread.
07-24 07:28:05.356: I/ActivityManager(382): Process example.atlcitylimo (pid 1358) has died.
07-24 07:28:05.436: I/WindowState(382): WIN DEATH: Window{b408a908 u0 example.atlcitylimo/example.atlcitylimo.Main}
07-24 07:28:05.526: I/WindowState(382): WIN DEATH: Window{b3fdbc30 u0 Toast}
我甚至在我的MANIFEST文件中添加了API密钥。我已导入Google Play库文件,几乎所有内容都已完成。但它没有运行。该项目直接崩溃。我也安装了Google Play服务。可以做些什么?有人可以帮忙吗?这是紧急情况。
答案 0 :(得分:0)
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
将此行添加到您的清单中。
答案 1 :(得分:0)
我在片段中实现了地图的类似问题。所以我制作了一个FrameLayout布局文件,并使用了com.google.android.gms.maps.MapView而不是Mapfragment。 你可以看看here
它也适用于支持库
答案 2 :(得分:0)
您的logcat说无法找到MapFragment
在您的XML更改android:name
更改为class
所以你的XML应该是这样的
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />