使用google maps android

时间:2015-05-06 05:54:03

标签: android google-maps

在处理具有2个活动MapPointActivity和MapReminderActivity的应用程序时。 我在MapPointActivity上有一个按钮,它有onClick事件移动到MapReminderActivity。

    public void gotoMap(View v){
            Intent intent = new Intent(this, MapReminderActivity.class);
            startActivity(intent);
        }

但是当我点击按钮时,我的应用程序崩溃了。 MapReminderActivity包含显示地图的代码。

MapReminderActivity

public class MapReminderActivity extends FragmentActivity implements
GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, LocationListener{
    private static final int GPS_ERRORDIALOG_REQUEST = 9001;
    GoogleMap mMap;
    GoogleApiClient mLocationClient;
    private Location mCurrentLocation;
    LocationRequest mLocationRequest;
    private static final float DEFAULT_ZOOM = 10;

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

            if (initMap()) {
                Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(this, "Map not available!", Toast.LENGTH_SHORT).show();
            }
        }
        else {
            setContentView(R.layout.activity_main);
        }

    }
    private void gotoLocation(double lat, double lng,
            float zoom) {
        LatLng ll = new LatLng(lat, lng);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
        mMap.moveCamera(update);
    }
    private void gotoLocation(double lat, double lng) {
        LatLng ll = new LatLng(lat, lng);
        CameraUpdate update = CameraUpdateFactory.newLatLng(ll);
        mMap.moveCamera(update);
    }
    public void geoLocate(View v) throws IOException {
        hideSoftKeyboard(v);

        EditText et = (EditText) findViewById(R.id.editText1);
        String location = et.getText().toString();

        Geocoder gc = new Geocoder(this);
        List<Address> list = gc.getFromLocationName(location, 1);
        Address add = list.get(0);
        String locality = add.getLocality();
        Toast.makeText(this, locality, Toast.LENGTH_LONG).show();

        double lat = add.getLatitude();
        double lng = add.getLongitude();

        gotoLocation(lat, lng, DEFAULT_ZOOM);

    }

    private void hideSoftKeyboard(View v) {
        InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
    public boolean servicesOK(){
        int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if(isAvailable == ConnectionResult.SUCCESS){
            return true;
        }
        else if(GooglePlayServicesUtil.isUserRecoverableError(isAvailable)){
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST);
            dialog.show();
        }
        else{
            Toast.makeText(this, "Can't connect to Google Play Services", Toast.LENGTH_LONG).show();
        }
        return false;
    }

    private boolean initMap() {
        if (mMap == null) {
            SupportMapFragment mapFrag =
                    (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1);
            mMap = mapFrag.getMap();
        }
        return (mMap != null);
    }
    @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) {
        mCurrentLocation = location;

    }


    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.d("Connection Falure Error",result.toString());

    }


    @Override
    public void onConnected(Bundle arg0) {
        if(servicesOK()) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, mLocationRequest, this);
        }

    }


    @Override
    public void onConnectionSuspended(int arg0) {
        // TODO Auto-generated method stub

    }

}

activity_map_reminder:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:text="Location:"
            android:textSize="20sp"
            android:layout_weight="20" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="128dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center|center_vertical"
            android:ems="10" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|center_vertical"
            android:onClick="geoLocate"
            android:text="Go" 
            android:layout_weight="30"/>
    </LinearLayout>

    <fragment
        android:id="@+id/map1"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="304dp"
        android:layout_height="fill_parent" >

    </fragment>

</LinearLayout>

我知道我的MapReminderActivity存在问题,但无法弄明白。

logcat的:

05-06 11:01:29.081: E/AndroidRuntime(3199): FATAL EXCEPTION: main
05-06 11:01:29.081: E/AndroidRuntime(3199): java.lang.IllegalStateException: Could not execute method of the activity
05-06 11:01:29.081: E/AndroidRuntime(3199):     at android.view.View$1.onClick(View.java:2179)
05-06 11:01:29.081: E/AndroidRuntime(3199):     at android.view.View.performClick(View.java:2535)
05-06 11:01:29.081: E/AndroidRuntime(3199):     at android.view.View$PerformClick.run(View.java:9129)
05-06 11:01:29.081: E/AndroidRuntime(3199):     at android.os.Handler.handleCallback(Handler.java:618)
05-06 11:01:29.081: E/AndroidRuntime(3199):     at android.os.Handler.dispatchMessage(Handler.java:123)
05-06 11:01:29.081: E/AndroidRuntime(3199):     at android.os.Looper.loop(SourceFile:351)
05-06 11:01:29.081: E/AndroidRuntime(3199):     at android.app.ActivityThread.main(ActivityThread.java:3820)
05-06 11:01:29.081: E/AndroidRuntime(3199):     at java.lang.reflect.Method.invokeNative(Native Method)
05-06 11:01:29.081: E/AndroidRuntime(3199):     at java.lang.reflect.Method.invoke(Method.java:538)
05-06 11:01:29.081: E/AndroidRuntime(3199):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
05-06 11:01:29.081: E/AndroidRuntime(3199):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
05-06 11:01:29.081: E/AndroidRuntime(3199):     at dalvik.system.NativeStart.main(Native Method)
05-06 11:01:29.081: E/AndroidRuntime(3199): Caused by: java.lang.reflect.InvocationTargetException
05-06 11:01:29.081: E/AndroidRuntime(3199):     at java.lang.reflect.Method.invokeNative(Native Method)
05-06 11:01:29.081: E/AndroidRuntime(3199):     at java.lang.reflect.Method.invoke(Method.java:538)
05-06 11:01:29.081: E/AndroidRuntime(3199):     at android.view.View$1.onClick(View.java:2174)
05-06 11:01:29.081: E/AndroidRuntime(3199):     ... 11 more
05-06 11:01:29.081: E/AndroidRuntime(3199): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.gps/com.example.gps.MapReminderActivity}; have you declared this activity in your AndroidManifest.xml?
05-06 11:01:29.081: E/AndroidRuntime(3199):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1445)
05-06 11:01:29.081: E/AndroidRuntime(3199):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1419)
05-06 11:01:29.081: E/AndroidRuntime(3199):     at android.app.Activity.startActivityForResult(Activity.java:2874)
05-06 11:01:29.081: E/AndroidRuntime(3199):     at android.app.Activity.startActivity(Activity.java:2980)
05-06 11:01:29.081: E/AndroidRuntime(3199):     at com.example.gps.MapPointActivity.gotoMap(MapPointActivity.java:18)
05-06 11:01:29.081: E/AndroidRuntime(3199):     ... 14 more

的Manifest.xml:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="10" 
        android:maxSdkVersion="17"/>
 <permission
        android:name="com.example.gps.permission.MAPS_RECIEVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.gps.permission.MAPS_RECIEVE" />
    <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-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.gps.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>
        <activity
            android:name="com.example.gps.AllReminderActivity"
            android:label="@string/title_activity_all_reminder" >
        </activity>
        <activity
            android:name="com.example.gps.CheckLocationActivity"
            android:label="@string/title_activity_check_location" >
        </activity>
        <activity
            android:name="com.example.gps.MapPointActivity"
            android:label="@string/title_activity_map_point" >
        </activity>
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyD8c_Jhw-x45hbYaf2LVTujSsQvzAuNaS4" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

根据logcat

: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.gps/com.example.gps.MapReminderActivity}; have you declared this activity in your AndroidManifest.xml?
 05-06 11:01:29.081: E/AndroidRuntime(3199):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1445)

您应该在MapReminderActivity标记下的manifest.xml注册<application>活动

 <activity
        android:name="com.example.gps.MapReminderActivity"
        android:configChanges="orientation" />