加载地图时应用程序停止

时间:2014-06-24 01:02:38

标签: java android google-maps

嗨我想要我的应用程序内的地图...... 我复制了三个教程,我为每个项目更改了密钥,结果是"应用已停止" 我使用Frangment或SupportFrangment .... 为什么我的三星S4没有执行应用程序看地图?

现在我解释一下我的应用程序在哪里复制并更改tutorial 前提: 我加载了google-play-service两次: 1-文件 - >导入 - >现有项目进入工作区 - > google-play-services并包含在每个项目中

2 - 在项目属性中添加库google-play-services。

哪里错了? 这是manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.com.vogella.android.locationapi.maps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="19" />


    <permission
        android:name="com.example.com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

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

    <uses-permission android:name="com.example.com.vogella.android.locationapi.maps.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/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.com.vogella.android.locationapi.maps.ShowMapActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="KeyVALUE API" />
    </application>

</manifest>

这是一个frangment.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"
    tools:context=".ShowMapActivity" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout>

这是一个ShowMapActivity.java:

    package com.example.com.vogella.android.locationapi.maps;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

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.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class ShowMapActivity extends Activity {
  static final LatLng HAMBURG = new LatLng(53.558, 9.927);
  static final LatLng KIEL = new LatLng(53.551, 9.993);
  private GoogleMap map;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_map);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap();
    Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
        .title("Hamburg"));
    Marker kiel = map.addMarker(new MarkerOptions()
        .position(KIEL)
        .title("Kiel")
        .snippet("Kiel is cool")
        .icon(BitmapDescriptorFactory
            .fromResource(R.drawable.ic_launcher)));

    // Move the camera instantly to hamburg with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

    // Zoom in, animating the camera.
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.show_map, menu);
    return true;
  }

} 

这是LogCat:

06-24 02:36:24.048: D/AndroidRuntime(6557): Shutting down VM
06-24 02:36:24.048: W/dalvikvm(6557): threadid=1: thread exiting with uncaught exception (group=0x418a9da0)
06-24 02:36:24.048: E/AndroidRuntime(6557): FATAL EXCEPTION: main
06-24 02:36:24.048: E/AndroidRuntime(6557): Process: com.example.com.vogella.android.locationapi.maps, PID: 6557
06-24 02:36:24.048: E/AndroidRuntime(6557): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.com.vogella.android.locationapi.maps/com.example.com.vogella.android.locationapi.maps.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.com.vogella.android.locationapi.maps.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.com.vogella.android.locationapi.maps-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.com.vogella.android.locationapi.maps-1, /vendor/lib, /system/lib]]
06-24 02:36:24.048: E/AndroidRuntime(6557):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
06-24 02:36:24.048: E/AndroidRuntime(6557):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
06-24 02:36:24.048: E/AndroidRuntime(6557):     at android.app.ActivityThread.access$900(ActivityThread.java:161)
06-24 02:36:24.048: E/AndroidRuntime(6557):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
06-24 02:36:24.048: E/AndroidRuntime(6557):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-24 02:36:24.048: E/AndroidRuntime(6557):     at android.os.Looper.loop(Looper.java:157)
06-24 02:36:24.048: E/AndroidRuntime(6557):     at android.app.ActivityThread.main(ActivityThread.java:5356)
06-24 02:36:24.048: E/AndroidRuntime(6557):     at java.lang.reflect.Method.invokeNative(Native Method)
06-24 02:36:24.048: E/AndroidRuntime(6557):     at java.lang.reflect.Method.invoke(Method.java:515)
06-24 02:36:24.048: E/AndroidRuntime(6557):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
06-24 02:36:24.048: E/AndroidRuntime(6557):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
06-24 02:36:24.048: E/AndroidRuntime(6557):     at dalvik.system.NativeStart.main(Native Method)
06-24 02:36:24.048: E/AndroidRuntime(6557): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.com.vogella.android.locationapi.maps.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.com.vogella.android.locationapi.maps-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.com.vogella.android.locationapi.maps-1, /vendor/lib, /system/lib]]
06-24 02:36:24.048: E/AndroidRuntime(6557):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
06-24 02:36:24.048: E/AndroidRuntime(6557):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
06-24 02:36:24.048: E/AndroidRuntime(6557):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
06-24 02:36:24.048: E/AndroidRuntime(6557):     at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
06-24 02:36:24.048: E/AndroidRuntime(6557):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2222)
06-24 02:36:24.048: E/AndroidRuntime(6557):     ... 11 more

更新LOGCAT:

 06-24 03:12:05.747: D/AndroidRuntime(11752): Shutting down VM
06-24 03:12:05.747: W/dalvikvm(11752): threadid=1: thread exiting with uncaught exception (group=0x418a9da0)
06-24 03:12:05.747: E/AndroidRuntime(11752): FATAL EXCEPTION: main
06-24 03:12:05.747: E/AndroidRuntime(11752): Process: com.example.com.vogella.android.locationapi.maps, PID: 11752
06-24 03:12:05.747: E/AndroidRuntime(11752): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.com.vogella.android.locationapi.maps/com.example.com.vogella.android.locationapi.maps.ShowMapActivity}: java.lang.NullPointerException
06-24 03:12:05.747: E/AndroidRuntime(11752):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
06-24 03:12:05.747: E/AndroidRuntime(11752):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
06-24 03:12:05.747: E/AndroidRuntime(11752):    at android.app.ActivityThread.access$900(ActivityThread.java:161)
06-24 03:12:05.747: E/AndroidRuntime(11752):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
06-24 03:12:05.747: E/AndroidRuntime(11752):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-24 03:12:05.747: E/AndroidRuntime(11752):    at android.os.Looper.loop(Looper.java:157)
06-24 03:12:05.747: E/AndroidRuntime(11752):    at android.app.ActivityThread.main(ActivityThread.java:5356)
06-24 03:12:05.747: E/AndroidRuntime(11752):    at java.lang.reflect.Method.invokeNative(Native Method)
06-24 03:12:05.747: E/AndroidRuntime(11752):    at java.lang.reflect.Method.invoke(Method.java:515)
06-24 03:12:05.747: E/AndroidRuntime(11752):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
06-24 03:12:05.747: E/AndroidRuntime(11752):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
06-24 03:12:05.747: E/AndroidRuntime(11752):    at dalvik.system.NativeStart.main(Native Method)
06-24 03:12:05.747: E/AndroidRuntime(11752): Caused by: java.lang.NullPointerException
06-24 03:12:05.747: E/AndroidRuntime(11752):    at com.example.com.vogella.android.locationapi.maps.ShowMapActivity.onCreate(ShowMapActivity.java:26)
06-24 03:12:05.747: E/AndroidRuntime(11752):    at android.app.Activity.performCreate(Activity.java:5426)
06-24 03:12:05.747: E/AndroidRuntime(11752):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
06-24 03:12:05.747: E/AndroidRuntime(11752):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
06-24 03:12:05.747: E/AndroidRuntime(11752):    ... 11 more

2 个答案:

答案 0 :(得分:1)

在您的AndroidManifest中,您说过您将提供名为MainActivity的课程,但您已将其重命名为ShowMapActivity,这意味着系统无法找到该活动它应该使用

答案 1 :(得分:0)

您需要使用较新的地图SupportMapFragment而不是MapFragment,并使用name标识符来查找SupportMapFragment的类。

<强>样品:

<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"
tools:context=".ShowMapActivity" >

<fragment
    android:id="@+id/map"
    android:name="com.androidmapsextensions.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

此外,您需要将地图的转换更改为SupportMapFragment

SupportMapFragment map;

map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
    .getMap();