Google Map中的NoClassDefFoundError

时间:2014-03-28 07:24:53

标签: android

我无法弄清楚错误:NoClassDefFoundError。我正在尝试创建一个简单的Google地图。 Google Play服务库有问题吗?

错误: -

 03-28 03:17:45.489: E/AndroidRuntime(2338): FATAL EXCEPTION: main
    03-28 03:17:45.489: E/AndroidRuntime(2338): Process: com.example.gpsdemo, PID: 2338
    03-28 03:17:45.489: E/AndroidRuntime(2338): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at android.app.Activity.onCreateView(Activity.java:4785)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at android.app.Activity.setContentView(Activity.java:1929)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at com.example.gpsdemo.MainActivity.onCreate(MainActivity.java:12)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at android.app.Activity.performCreate(Activity.java:5231)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at android.os.Handler.dispatchMessage(Handler.java:102)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at android.os.Looper.loop(Looper.java:136)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at android.app.ActivityThread.main(ActivityThread.java:5017)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at java.lang.reflect.Method.invokeNative(Native Method)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at java.lang.reflect.Method.invoke(Method.java:515)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    03-28 03:17:45.489: E/AndroidRuntime(2338):     at dalvik.system.NativeStart.main(Native Method)

MainActivity.java

package com.example.gpsdemo;


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

public class MainActivity extends Activity {

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

actvity_main.xml:

<?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"
          android:name="com.google.android.gms.maps.MapFragment"/>

更改后出错: -

03-28 03:45:52.759: E/AndroidRuntime(2625): FATAL EXCEPTION: main
03-28 03:45:52.759: E/AndroidRuntime(2625): Process: com.example.gpsdemo, PID: 2625
03-28 03:45:52.759: E/AndroidRuntime(2625): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gpsdemo/com.example.gpsdemo.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.os.Looper.loop(Looper.java:136)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at java.lang.reflect.Method.invoke(Method.java:515)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at dalvik.system.NativeStart.main(Native Method)
03-28 03:45:52.759: E/AndroidRuntime(2625): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.app.Activity.setContentView(Activity.java:1929)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at com.example.gpsdemo.MainActivity.onCreate(MainActivity.java:26)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.app.Activity.performCreate(Activity.java:5231)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-28 03:45:52.759: E/AndroidRuntime(2625):     ... 11 more
03-28 03:45:52.759: E/AndroidRuntime(2625): 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
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.app.Fragment.instantiate(Fragment.java:597)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.app.Fragment.instantiate(Fragment.java:561)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.app.Activity.onCreateView(Activity.java:4778)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
03-28 03:45:52.759: E/AndroidRuntime(2625):     ... 20 more
03-28 03:45:52.759: E/AndroidRuntime(2625): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: DexPathList[[zip file "/data/app/com.example.gpsdemo-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.gpsdemo-2, /system/lib]]
03-28 03:45:52.759: E/AndroidRuntime(2625):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
03-28 03:45:52.759: E/AndroidRuntime(2625):     at android.app.Fragment.instantiate(Fragment.java:583)
03-28 03:45:52.759: E/AndroidRuntime(2625):     ... 23 more

这是清单文件: -

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

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

     <uses-permission android:name="android.permission.INTERNET"/>
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
     <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"/>


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

      <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
         <activity
            android:name="com.example.gpsdemo.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>


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


    </application>

</manifest>

3 个答案:

答案 0 :(得分:0)

像这样更改MainActivity

 import android.os.Bundle;
 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 MainActivity extends Activity {


private GoogleMap googleMap;
MapFragment mapFragment;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main );
    mapFragment = (MapFragment) getFragmentManager().findFragmentById(
            R.id.map);
          googleMap = mapFragment.getMap();
          googleMap.setMyLocationEnabled(true);


}

   }

\和这样的清单权限

   <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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" />

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

  <!-and metadata above--> </application>


 <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="your android API key for map" />

答案 1 :(得分:0)

答案 2 :(得分:0)

使用FragmentActivity而不是Activity

将此代码放在ApplicationManifest文件的Application tag

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