-1

谁能澄清我的错误在哪里?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 APIs”。同样,当我不尝试显示地图时,应用程序运行良好。

以下是 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 New LogCat since changed android: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       
4

5 回答 5

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

像这样在 AndroidManifest 中定义您的活动

<activity
      android:name="com.example.myfirstapp.MainActivity"
      android:label="@string/app_name">
</activity>
于 2013-01-11T07:15:02.633 回答
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();
     }
   }
于 2013-01-11T07:13:08.880 回答
1

必须使用SupportMapFragmentifAPI 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扩展您的Activity

    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();
     }
    }
    
于 2013-01-11T07:25:33.710 回答
0

更改 AndroidManifest.xml

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

并给予地图和互联网的许可。

于 2013-01-11T07:49:15.813 回答
0

几天来我一直遇到同样的问题,克隆这个 repo 对我有很大帮助,

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

祝你好运

于 2013-10-16T04:40:44.253 回答