Runtime errors while Launching the Map

2019-08-09 17:32发布

问题:

Here's my onCreate() Method..The two commented lines were used previously to launch the map and it worked fine.Later I found out Google doen't use LocationClient anymore and they use GoogleAPIClient instead.So I changed it.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button send = (Button) findViewById(R.id.button2);

        if(initMap())
        {  
            //mLocationClient = new LocationClient(this,this,this);
            //mLocationClient.connect();
            mGoogleApiClient =new GoogleApiClient.Builder(MainActivity.this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(MainActivity.this)
            .addOnConnectionFailedListener(MainActivity.this)
            .build();
        }
        else
        {
            Toast.makeText(this,"Map Not Available!",Toast.LENGTH_SHORT).show();
        }
    }

This is initMap Method

private boolean initMap(){
        if(mMap==null)
        {
            //making Reference to Map Object
            SupportMapFragment mapFrag = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 
            mMap=mapFrag.getMap();
        }
        return(mMap!=null);
    }

This is the logcat report.

12-20 15:45:47.195: E/AndroidRuntime(1457): FATAL EXCEPTION: main
12-20 15:45:47.195: E/AndroidRuntime(1457): Process: com.example.mapsproject, PID: 1457
12-20 15:45:47.195: E/AndroidRuntime(1457): java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/R$styleable;
12-20 15:45:47.195: E/AndroidRuntime(1457):     at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2158)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.app.Activity.setContentView(Activity.java:2144)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at com.example.mapsproject.MainActivity.onCreate(MainActivity.java:71)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.app.Activity.performCreate(Activity.java:5933)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.os.Looper.loop(Looper.java:135)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.app.ActivityThread.main(ActivityThread.java:5221)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at java.lang.reflect.Method.invoke(Native Method)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at java.lang.reflect.Method.invoke(Method.java:372)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-20 15:45:47.195: E/AndroidRuntime(1457): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.R$styleable" on path: DexPathList[[zip file "/data/app/com.example.mapsproject-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
12-20 15:45:47.195: E/AndroidRuntime(1457):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
12-20 15:45:47.195: E/AndroidRuntime(1457):     ... 25 more
12-20 15:45:47.195: E/AndroidRuntime(1457):     Suppressed: java.lang.ClassNotFoundException: com.google.android.gms.R$styleable
12-20 15:45:47.195: E/AndroidRuntime(1457):         at java.lang.Class.classForName(Native Method)
12-20 15:45:47.195: E/AndroidRuntime(1457):         at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
12-20 15:45:47.195: E/AndroidRuntime(1457):         at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
12-20 15:45:47.195: E/AndroidRuntime(1457):         at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
12-20 15:45:47.195: E/AndroidRuntime(1457):         ... 26 more
12-20 15:45:47.195: E/AndroidRuntime(1457):     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
12-20 15:45:51.501: I/Process(1457): Sending signal. PID: 1457 SIG: 9

回答1:

Here is the problem your activity is there alright, but Eclipse didn't include the support package in the APK ... If you look above your error in stacktrace you will notice the root of your problem:

 Didn't find class "com.google.android.gms.R$styleable" on path: DexPathList[[zip file "/data/app/com.example.mapsproject-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

So you can unpack the apk, undex it and you will see the compatibility package classes were not included.

To fix this

Using Eclipse

  • right-click your project,
  • properties
  • build path
  • go to export tab.
  • make sure "Android private libraries" are checked.
  • A clean and rebuild should put you on track ...

Using Android Studio

After upgrading to Android Stuido 0.8.2, I had this problem. I tried a lot of things, including

  • Clean project
  • Clean project (gradle clean)
  • Invalidate Caches and Restart
  • De-qualify the Activity Name in AndroidManifest.xml (.MyActivity)

Nothing worked until I re-imported the entire project.

File -> Close Project -> Import Project... -> Choose the same project -> OK

Hope this Help if not please comment below. :)