不幸的是,我的应用程序已停止工作

时间:2013-01-11 07:07:33

标签: android eclipse google-maps

有人可以澄清我的错误在哪里吗? Android应用程序因错误而崩溃,“不幸的是,我的应用程序已停止工作”。只有当我的activity_main.xml包含android:id="@+id/map"时才会发生崩溃:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.MapFragment"/>

我的MainActivity.java类同样简单:

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

我添加了“google-play”依赖项,我的目标是“Google API”。再次,当我不尝试显示地图时,应用程序运行良好。

以下是LogCat错误:

01-11 02:04:24.608: E/AndroidRuntime(14416): FATAL EXCEPTION: main
01-11 02:04:24.608: E/AndroidRuntime(14416): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}: java.lang.ClassNotFoundException: com.example.myfirstapp.MainActivity
01-11 02:04:24.608: E/AndroidRuntime(14416):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
01-11 02:04:24.608: E/AndroidRuntime(14416):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
01-11 02:04:24.608: E/AndroidRuntime(14416):    at android.app.ActivityThread.access$600(ActivityThread.java:151)
01-11 02:04:24.608: E/AndroidRuntime(14416):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
01-11 02:04:24.608: E/AndroidRuntime(14416):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 02:04:24.608: E/AndroidRuntime(14416):    at android.os.Looper.loop(Looper.java:155)
01-11 02:04:24.608: E/AndroidRuntime(14416):    at android.app.ActivityThread.main(ActivityThread.java:5485)
01-11 02:04:24.608: E/AndroidRuntime(14416):    at java.lang.reflect.Method.invokeNative(Native Method)
01-11 02:04:24.608: E/AndroidRuntime(14416):    at java.lang.reflect.Method.invoke(Method.java:511)
01-11 02:04:24.608: E/AndroidRuntime(14416):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
01-11 02:04:24.608: E/AndroidRuntime(14416):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
01-11 02:04:24.608: E/AndroidRuntime(14416):    at dalvik.system.NativeStart.main(Native Method)
01-11 02:04:24.608: E/AndroidRuntime(14416): Caused by: java.lang.ClassNotFoundException: com.example.myfirstapp.MainActivity
01-11 02:04:24.608: E/AndroidRuntime(14416):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
01-11 02:04:24.608: E/AndroidRuntime(14416):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
01-11 02:04:24.608: E/AndroidRuntime(14416):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
01-11 02:04:24.608: E/AndroidRuntime(14416):    at android.app.Instrumentation.newActivity(Instrumentation.java:1069)
01-11 02:04:24.608: E/AndroidRuntime(14416):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
01-11 02:04:24.608: E/AndroidRuntime(14416):    ... 11 more

更新 这是清单

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

    <!-- MAPS -->
    <permission android:name="com.example.myfirstapp.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
    <uses-permission android:name="com.example.myfirstapp.permission.MAPS_RECEIVE"/>

    <uses-permission android:name="android.permission.INTERNET"/>

    <!-- Allows the API to access Google web-based services -->
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

    <!-- External storage for caching. -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <!-- My Location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <!-- Maps API needs OpenGL ES 2.0. -->
    <uses-feature
      android:glEsVersion="0x00020000"
      android:required="true"/>
    <!-- 8/17 oneX=16-->
    <uses-sdk
        android:minSdkVersion="12" 
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <!-- REMOVED user-library android:name="com.google.android.maps" / -->

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

        <activity
            android:name="com.example.myfirstapp.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>
    </application>

</manifest>

UDPATED 自更改android以来新的LogCat:minSdkVersion =“12”“仅当您使用的是API 12及更高版本时才使用MapFragment。否则,请使用SupportMapFragment。”

01-11 11:26:23.267: E/AndroidRuntime(27646): FATAL EXCEPTION: main
01-11 11:26:23.267: E/AndroidRuntime(27646): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.app.ActivityThread.access$600(ActivityThread.java:151)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.os.Looper.loop(Looper.java:155)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.app.ActivityThread.main(ActivityThread.java:5485)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at java.lang.reflect.Method.invokeNative(Native Method)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at java.lang.reflect.Method.invoke(Method.java:511)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at dalvik.system.NativeStart.main(Native Method)
01-11 11:26:23.267: E/AndroidRuntime(27646): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:358)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.app.Activity.setContentView(Activity.java:1912)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at com.example.myfirstapp.MainActivity.onCreate(MainActivity.java:12)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.app.Activity.performCreate(Activity.java:5066)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
01-11 11:26:23.267: E/AndroidRuntime(27646):    ... 11 more
01-11 11:26:23.267: E/AndroidRuntime(27646): 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
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.app.Fragment.instantiate(Fragment.java:624)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.app.Fragment.instantiate(Fragment.java:584)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.app.Activity.onCreateView(Activity.java:4714)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
01-11 11:26:23.267: E/AndroidRuntime(27646):    ... 21 more
01-11 11:26:23.267: E/AndroidRuntime(27646): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment
01-11 11:26:23.267: E/AndroidRuntime(27646):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
01-11 11:26:23.267: E/AndroidRuntime(27646):    at android.app.Fragment.instantiate(Fragment.java:606)
01-11 11:26:23.267: E/AndroidRuntime(27646):    ... 24 more       

5 个答案:

答案 0 :(得分:2)

Caused by: java.lang.ClassNotFoundException: com.example.myfirstapp.MainActivity

在AndroidManifest中定义您的活动

<activity
      android:name="com.example.myfirstapp.MainActivity"
      android:label="@string/app_name">
</activity>

答案 1 :(得分:1)

由于您已定义android:minSdkVersion="11"MapFragment仅在api 12或更高版本上受支持。

因此,您必须使用SupportMapFragment代替MapFragment,如下所示:

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

不要忘记使用FragmentActivity扩展您的活动。

    public class MainActivity extends FragmentActivity{

           SupportMapFragment mMap;
           GoogleMap googleMap;

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

             mMap = (SupportMapFragment) getSupportFragmentManager()
                  .findFragmentById(R.id.map);

               googleMap = mMap.getMap();
     }
   }

答案 2 :(得分:1)

如果SupportMapFragment

,则必须使用API version is below 11
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
  • 不要忘记使用 FragmentActivity 扩展您的活动

    public class MainActivity extends FragmentActivity{
    
    SupportMapFragment mMap;
    GoogleMap googleMap;
    
      @Override
     protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
           mMap = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    
            googleMap = mMap.getMap();
     }
    }
    

答案 3 :(得分:0)

更改AndroidManifest.xml

<activity
      android:name="com.example.myfirstapp.MainActivity"
      android:label="@string/app_name">
</activity>

并获得地图和互联网的许可。

答案 4 :(得分:0)

我在某些日子里遇到了同样的问题,克隆这个回购帮助了我很多,

https://github.com/googlemaps/hellomap-android

祝你好运