Force Close缺少com.google.android.gms.maps

时间:2014-03-18 08:01:09

标签: java android eclipse google-maps google-api

我在我的应用程序中使用maps API,每当我启动活动时,我都会获得一个FC。源代码:

package com.spxc.nightclubratings;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
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.spxc.nightclubratings.adapter.MainMapFragement;
import com.spxc.nightclubratings.classes.EventInfo;
import com.spxc.nightclubratings.parser.JSONParser;

public class MapsActivity extends Activity {

     private MainMapFragement mapFragment;
     private HashMap<Marker, EventInfo> eventMarkerMap;
     private LatLng mPosition;

     Geocoder geocoder;
     String bestProvider;
     List<Address> user = null;
     double lat, lng;

     JSONParser jParser = new JSONParser();

     ArrayList<HashMap<String, String>> locationList;

     // JSON Node names
     private static final String TAG_SUCCESS = "success";
     private static final String TAG_LOCATIONS = "locations";
     private static final String TAG_ID = "id";
     private static final String TAG_NAME = "name";
     private static final String TAG_LOCATION = "location";
     private static final String TAG_LAT = "lat";
     private static final String TAG_LNG = "lng";
     private static final String TAG_VERIFIED = "verified";
     private static final String TAG_TRIBE = "tribe";
     private static final String TAG_FEATURED = "featured";

     private static String url_all_products = "http://appwhittle.com/api/db_all.php";

     String id, name, verified, latS, lngS;

     // products JSONArray
     JSONArray products = null;

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

        ActionBar abs = getActionBar();
        abs.setTitle(R.string.activity_title_map);
        abs.setDisplayHomeAsUpEnabled(true);
        abs.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#2c71b6")));

        mapFragment = new MainMapFragement();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(R.id.map, mapFragment);
        ft.commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

     @Override
     protected void onStart() {
         super.onStart();
         new LoadAllLocations().execute();
         //setUpEventSpots();
         setCurrentLocationSpot();
     }

     private void setCurrentLocationSpot() {
         LocationManager lm = (LocationManager) MapsActivity.this.getSystemService(Context.LOCATION_SERVICE);

         Criteria criteria = new Criteria();
         bestProvider = lm.getBestProvider(criteria, false);
         Location location = lm.getLastKnownLocation(bestProvider);

         if (location == null){
             Toast.makeText(MapsActivity.this,"Location Not found",Toast.LENGTH_LONG).show();
          }else{
              geocoder = new Geocoder(MapsActivity.this);
              try {
                  user = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                  lat = (double)user.get(0).getLatitude();
                  lng = (double)user.get(0).getLongitude();

                  mPosition = new LatLng(lat, lng);

                  GoogleMap mGap = ((MapFragment) getFragmentManager()
                          .findFragmentById(R.id.map)).getMap();

                  mGap.setMyLocationEnabled(true);
                  mGap.animateCamera(CameraUpdateFactory.newLatLngZoom(mPosition, 13));

              }catch (Exception e) {
                  e.printStackTrace();
              }
          }
     }

     class LoadAllLocations extends AsyncTask<String, String, String> {

            /**
             * Before starting background thread Show Progress Dialog
             * */
            @Override
            protected void onPreExecute() {
                super.onPreExecute();                           
            }

            protected String doInBackground(String... args) {
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

                try {
                    int success = json.getInt(TAG_SUCCESS);

                    if (success == 1) {
                        products = json.getJSONArray(TAG_LOCATIONS);

                    } else {
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return null;
            }

            protected void onPostExecute(String file_url) {
                runOnUiThread(new Runnable() {
                    public void run() {

                        for (int i = 0; i < products.length(); i++) {


                            try {
                                JSONObject c = products.getJSONObject(i);
                                id = c.getString(TAG_ID);
                                name = c.getString(TAG_NAME);
                                latS = c.getString(TAG_LAT);
                                lngS = c.getString(TAG_LNG);
                                int verified = c.getInt(TAG_VERIFIED);
                                int tribe = c.getInt(TAG_TRIBE);
                                int featured = c.getInt(TAG_FEATURED);
                                if (verified == 1) {
                                     Double l1;
                                        Double l2;
                                        l1 = Double.parseDouble(latS);
                                        l2 = Double.parseDouble(lngS);

                                        EventInfo eventInfo = new EventInfo(new LatLng(l1, l2), name, new Date(), null);
                                        Marker mMarker = mapFragment.placeMarker(eventInfo);
                                        switch(tribe) {
                                        case 1:
                                            if (featured == 1) {
                                                mMarker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
                                            } else {
                                                mMarker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
                                            }

                                            eventMarkerMap = new HashMap<Marker, EventInfo>();
                                            eventMarkerMap.put(mMarker, eventInfo);
                                            break;
                                        case 2:
                                            if (featured == 1) {
                                                mMarker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
                                            } else {
                                                mMarker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
                                            }
                                            eventMarkerMap = new HashMap<Marker, EventInfo>();
                                            eventMarkerMap.put(mMarker, eventInfo);
                                            break;
                                        }

                                }else{

                                }
                                mapFragment.getMap().setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
                                      public void onInfoWindowClick(Marker mMarker) {
                                          EventInfo eventInfo = eventMarkerMap.get(mMarker);
                                          Toast.makeText(getBaseContext(),
                                          "The date of " + eventInfo.getName() + " is " + id,
                                          Toast.LENGTH_LONG).show();
                                      }
                                      });
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }


                        }


                    }
                });


            }
     }
}

我的Logcat:

03-18 08:51:29.742: E/AndroidRuntime(8948): FATAL EXCEPTION: main
03-18 08:51:29.742: E/AndroidRuntime(8948): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spxc.nightclubratings/com.spxc.nightclubratings.MapsActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.app.ActivityThread.access$700(ActivityThread.java:159)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.os.Looper.loop(Looper.java:137)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.app.ActivityThread.main(ActivityThread.java:5419)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at java.lang.reflect.Method.invokeNative(Native Method)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at java.lang.reflect.Method.invoke(Method.java:525)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at dalvik.system.NativeStart.main(Native Method)
03-18 08:51:29.742: E/AndroidRuntime(8948): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.app.Activity.setContentView(Activity.java:1956)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at com.spxc.nightclubratings.MapsActivity.onCreate(MapsActivity.java:77)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.app.Activity.performCreate(Activity.java:5372)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
03-18 08:51:29.742: E/AndroidRuntime(8948):     ... 11 more
03-18 08:51:29.742: E/AndroidRuntime(8948): 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-18 08:51:29.742: E/AndroidRuntime(8948):     at android.app.Fragment.instantiate(Fragment.java:596)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.app.Fragment.instantiate(Fragment.java:560)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.app.Activity.onCreateView(Activity.java:4958)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
03-18 08:51:29.742: E/AndroidRuntime(8948):     ... 20 more
03-18 08:51:29.742: E/AndroidRuntime(8948): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: /data/app/com.spxc.nightclubratings-1.apk
03-18 08:51:29.742: E/AndroidRuntime(8948):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:64)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
03-18 08:51:29.742: E/AndroidRuntime(8948):     at android.app.Fragment.instantiate(Fragment.java:582)
03-18 08:51:29.742: E/AndroidRuntime(8948):     ... 23 more

XML:

<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" />

有什么问题?我添加了谷歌播放库。

非常感谢任何帮助:)

1 个答案:

答案 0 :(得分:0)

我通过添加google play lib以及清理我的项目并在libs文件夹中添加jar来解决问题:)