0

每次我尝试在我的手机上测试我的应用程序时,它都会显示一条提示“不幸的是,该应用程序已停止”。

我已经检查了构建路径,并确保添加了 Google Play 服务库。我已经尝试搜索几个关于如何修复的线程,但它们都没有工作。所以我把这篇文章作为最后的手段让别人手动检查。

显现:

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

    <permission
        android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" />

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

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        >
        <uses-library android:name="com.google.android.maps"/>
        <activity
            android:name="info.androidhive.googlemapsv2.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppBaseTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <!-- Goolge API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="My KEY" />
    </application>

</manifest>

活动主.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

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

主要活动.java:

package info.androidhive.info;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

import info.androidhive.googlemapsv2.R;




import android.os.Bundle;
import android.support.v4.app.FragmentActivity;


public class MainActivity extends FragmentActivity {

    private SupportMapFragment mapFragment;
    private GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_main);

        /*A noter que dans le cas où nous sommes sur un device< 3.0 nous devrions utiliser les librairies de compatibilités (FragmentActivity, SupportMapfragment).*/
        mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        googleMap = mapFragment.getMap();
        googleMap.setMyLocationEnabled(true);

    }



}

日志猫:

10-31 18:26:51.220: E/AndroidRuntime(869): FATAL EXCEPTION: main
10-31 18:26:51.220: E/AndroidRuntime(869): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{info.androidhive.googlemapsv2/info.androidhive.googlemapsv2.MainActivity}: java.lang.ClassNotFoundException: info.androidhive.googlemapsv2.MainActivity in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/info.androidhive.googlemapsv2-2.apk]
10-31 18:26:51.220: E/AndroidRuntime(869):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2703)
10-31 18:26:51.220: E/AndroidRuntime(869):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
10-31 18:26:51.220: E/AndroidRuntime(869):  at android.app.ActivityThread.access$2300(ActivityThread.java:135)
10-31 18:26:51.220: E/AndroidRuntime(869):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
10-31 18:26:51.220: E/AndroidRuntime(869):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 18:26:51.220: E/AndroidRuntime(869):  at android.os.Looper.loop(Looper.java:143)
10-31 18:26:51.220: E/AndroidRuntime(869):  at android.app.ActivityThread.main(ActivityThread.java:4914)
10-31 18:26:51.220: E/AndroidRuntime(869):  at java.lang.reflect.Method.invokeNative(Native Method)
10-31 18:26:51.220: E/AndroidRuntime(869):  at java.lang.reflect.Method.invoke(Method.java:521)
10-31 18:26:51.220: E/AndroidRuntime(869):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
10-31 18:26:51.220: E/AndroidRuntime(869):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-31 18:26:51.220: E/AndroidRuntime(869):  at dalvik.system.NativeStart.main(Native Method)
10-31 18:26:51.220: E/AndroidRuntime(869): Caused by: java.lang.ClassNotFoundException: info.androidhive.googlemapsv2.MainActivity in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/info.androidhive.googlemapsv2-2.apk]
10-31 18:26:51.220: E/AndroidRuntime(869):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
10-31 18:26:51.220: E/AndroidRuntime(869):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
10-31 18:26:51.220: E/AndroidRuntime(869):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
10-31 18:26:51.220: E/AndroidRuntime(869):  at android.app.Instrumentation.newActivity(Instrumentation.java:1033)
10-31 18:26:51.220: E/AndroidRuntime(869):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695)
10-31 18:26:51.220: E/AndroidRuntime(869):  ... 11 more
4

3 回答 3

0

尝试在 setContentView() 之后立即添加这部分代码:

int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (result!=ConnectionResult.SUCCESS) {
    GooglePlayServicesUtil.getErrorDialog(result, this, 23);
    this.finish();
}

以验证您对 GooglePlayServices 没有任何问题。现在用这个改变你在 onCreate() 中的代码:

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
GoogleMap googleMap = fragment.getMap();

您的清单似乎正确!

于 2013-10-31T20:08:01.957 回答
0

您好,我过去也遇到过同样的错误。

请修改您的代码

mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    googleMap = mapFragment.getMap();

 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();
                }
            }

并添加清单 googleplayservice 版本

 <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

并检查您的包裹名称。我希望这能帮到您。

于 2014-10-15T11:50:28.470 回答
0

您将清单中的活动称为

 <activity
        android:name="info.androidhive.googlemapsv2.MainActivity"
        android:label="@string/app_name"

而你Activity在包裹中

package info.androidhive.info;

所以将上面的清单代码更改为

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

    <permission
        android:name="info.androidhive.info.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission  android:name="info.androidhive.info.permission.MAPS_RECEIVE" />

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

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        >
        <uses-library android:name="com.google.android.maps"/>
        <activity
            android:name="info.androidhive.info.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppBaseTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <!-- Goolge API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="My KEY" />
    </application>

</manifest>

或者您可以简单地将您的包名称折射为:

info.androidhive.googlemapsv2;

并将其在您的活动中的声明更改为:

package info.androidhive.googlemapsv2;
于 2013-10-31T18:32:51.897 回答