android - 地图应用程序停止工作

时间:2014-03-23 11:22:34

标签: android

我不明白这段代码的问题。看起来正确但不起作用。应用程序在运行时停止工作。请帮忙。

我尝试调试应用但没有运气。我想要做的是打开这个应用程序,以便在LatLng指定的地图上的特定位置。

    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.GooglePlayServicesClient;
    import com.google.android.gms.location.LocationClient;
    import com.google.android.gms.location.LocationListener;
    import com.google.android.gms.location.LocationRequest;
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.GoogleMapOptions;
    import com.google.android.gms.maps.MapFragment;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.LatLngCreator;

    import android.location.Location;
    import android.os.Bundle;
    import android.app.Activity;
    import android.util.Log;
    import android.view.Menu;
    import android.widget.Toast;

    public class MainActivity extends Activity implements
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener, LocationListener{        

        private GoogleMap mMap;
        public android.location.Location mCurrentLocation;
        GoogleMapOptions options = new GoogleMapOptions();
        double latitude, longitude;
        public LocationClient mLocationClient;
        public LocationRequest mLocationRequest;

          static final LatLng PES = new LatLng(12.9338249,77.5332331);

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            final GoogleMap mMap;
            Log.i("LL", "done 0");

            mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            Log.i("LL", "done1");

            MapSetUp();
            Log.i("LL", "donethis2");

            mMap.animateCamera(CameraUpdateFactory.newLatLng(PES));
            Log.i("LL", "donetgis3");

        }

        public void MapSetUp(){
            if (mMap == null) {
                mMap = ((MapFragment) getFragmentManager().findFragmentById(
                        R.id.map)).getMap();
            }
            if (mMap != null) {
                mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                mMap.setMyLocationEnabled(true);
                mMap.getUiSettings().setZoomControlsEnabled(true);
                mMap.getUiSettings().setMyLocationButtonEnabled(true);
                mMap.getUiSettings().setCompassEnabled(true);
                mMap.getUiSettings().setRotateGesturesEnabled(true);
                mMap.getUiSettings().setZoomGesturesEnabled(true);
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            String msg = "Updated Location: "
                    + Double.toString(location.getLatitude()) + ","
                    + Double.toString(location.getLongitude());
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onConnectionFailed(ConnectionResult arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onConnected(Bundle arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onDisconnected() {
            // TODO Auto-generated method stub
            Toast.makeText(this, "Not Connected", Toast.LENGTH_SHORT).show();

        }

    }

logcat的

        03-23 17:37:19.505: W/dalvikvm(7265): threadid=1: thread exiting with uncaught                                         exception (group=0x416a4d40)

        03-23 17:37:19.510: E/AndroidRuntime(7265): FATAL EXCEPTION: main

        03-23 17:37:19.510: E/AndroidRuntime(7265): Process: com.pes.nikhil, PID: 7265

        03-23 17:37:19.510: E/AndroidRuntime(7265): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pes.nikhil/com.pes.nikhil.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at android.app.ActivityThread.access$800(ActivityThread.java:139)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at android.os.Handler.dispatchMessage(Handler.java:102)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at android.os.Looper.loop(Looper.java:136)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at android.app.ActivityThread.main(ActivityThread.java:5102)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at java.lang.reflect.Method.invokeNative(Native Method)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at java.lang.reflect.Method.invoke(Method.java:515)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at dalvik.system.NativeStart.main(Native Method)

          03-23 17:37:19.510: E/AndroidRuntime(7265): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at android.app.Activity.setContentView(Activity.java:1937)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at com.pes.nikhil.MainActivity.onCreate(MainActivity.java:38)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at android.app.Activity.performCreate(Activity.java:5248)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     ... 11 more

        03-23 17:37:19.510: E/AndroidRuntime(7265): Caused by: java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)

         03-23 17:37:19.510: E/AndroidRuntime(7265):    at com.google.android.gms.maps.internal.q.v(Unknown Source)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at com.google.android.gms.maps.internal.q.u(Unknown Source)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at com.google.android.gms.maps.MapFragment$b.eb(Unknown Source)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at com.google.android.gms.dynamic.a.a(Unknown Source)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at com.google.android.gms.dynamic.a.onInflate(Unknown Source)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)

        03-23 17:37:19.510: E/AndroidRuntime(7265):     at android.app.Activity.onCreateView(Activity.java:4802)
03-23 17:37:19.510: E/AndroidRuntime(7265):     at                 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
        03-23 17:37:19.510: E/AndroidRuntime(7265):     ... 20 more

1 个答案:

答案 0 :(得分:1)

它告诉你堆栈跟踪有什么问题:

A required meta-data tag in your app's AndroidManifest.xml does not exist.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

您需要在Manifest文件中添加指定的标记。