0

我正在尝试开发一个具有按钮的应用程序,单击该按钮应将我带到地图中的当前位置。我的主要困惑是 OnClick 侦听器和地图事件。我哪里可能出错?

这是我的代码:-

package geopoint.ns;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;


public class GeoPointActivity extends MapActivity {

MapView mapView; 
MapController mc;
GeoPoint p;
TextView tvlat,tvlong;

protected LocationManager locationManager;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mapView = (MapView) findViewById(R.id.mapView);
    mapView.setBuiltInZoomControls(true);
    //mapView.displayZoomControls(true);
    mapView.setStreetView(false);
    mapView.setSatellite(false);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            MINIMUM_TIME_BETWEEN_UPDATES, 
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
            new MyLocationListener()
    );

    tvlat = (TextView) findViewById(R.layout.main);
    tvlong = (TextView) findViewById(R.layout.main);
    mc = mapView.getController();
    addListenerOnButton();

}

public void addListenerOnButton() {
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            getLoc();
        }
    });
}

@Override
protected boolean isRouteDisplayed() {
    return false;
}

class MapOverlay extends com.google.android.maps.Overlay implements OnClickListener
{
    @Override
    public boolean draw(Canvas canvas, MapView mapView, 
    boolean shadow, long when) 
    {
        super.draw(canvas, mapView, shadow);                   

        //---translate the GeoPoint to screen pixels---
        Point screenPts = new Point();
        mapView.getProjection().toPixels(p, screenPts);

        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) 
    {   
         //---when user lifts his finger---
        if (event.getAction() == 1) {                
            GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY());

            Geocoder geoCoder = new Geocoder(
                    getBaseContext(), Locale.getDefault());

            try 
            {
                List<Address> addresses = geoCoder.getFromLocation(
                    p.getLatitudeE6()/1E6, 
                    p.getLongitudeE6()/1E6, 1);

                String add = "";
                if (addresses.size() > 0) 
                {
                    for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
                         i++)
                       add += addresses.get(0).getAddressLine(i) + "\n";
                }

                Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
            }
            catch (IOException e) {                
                e.printStackTrace();
            }
            return true;
            //Toast.makeText(getBaseContext(), p.getLatitudeE6() / 1E6 + "," +p.getLongitudeE6() /1E6 ,Toast.LENGTH_SHORT).show();
        }
        else
            return false;
    }

    @Override
    public void onClick(View arg0) {
            getLoc();
        }
}

protected void getLoc () {
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    double lat = location.getLatitude();
    double lng = location.getLongitude();

    p = new GeoPoint(
        (int) (lat * 1E6), 
        (int) (lng * 1E6)); 
    mc.animateTo(p);
    mc.setZoom(17); 
}

 public class MyLocationListener implements LocationListener
 {
     @Override
     public void onLocationChanged(Location loc)
     {
     loc.getLatitude();
     loc.getLongitude();
     String Text = "My current location is: " +
     "Latitud = " + loc.getLatitude() +
     "Longitud = " + loc.getLongitude();
     Toast.makeText( getApplicationContext(),
     Text,
     Toast.LENGTH_SHORT).show();

     tvlat.setText(""+loc.getLatitude());
     tvlong.setText(""+loc.getLongitude());

     this.gpsCurrentLocation();
     }

     public void gpsCurrentLocation()
     {
     String coordinates[] = {""+tvlat.getText(), ""+tvlong.getText()};

     double lat = Double.parseDouble(coordinates[0]);
     double lng = Double.parseDouble(coordinates[1]);

     GeoPoint p = new GeoPoint(
     (int) (lat * 1E6),
     (int) (lng * 1E6));

     mc.animateTo(p);
     mc.setZoom(7);

     mapView.invalidate();

     }

     @Override
     public void onProviderDisabled(String provider)
     {
         Toast.makeText( getApplicationContext(),
         "Gps Disabled",
         Toast.LENGTH_SHORT ).show();
     }

     @Override
     public void onProviderEnabled(String provider)
     {
         Toast.makeText( getApplicationContext(),
         "Gps Enabled",
         Toast.LENGTH_SHORT).show();
     }

     @Override
     public void onStatusChanged(String provider, int status, Bundle extras)
     {

     }

 }

}

在清单文件中,我拥有 GPS(ACCESS_FINE_LOCATION、ACCESS_MOCK_LOCATION 和 ACCESS_COARSE_LOCATION)和 Internet 所需的所有权限。

我的堆栈跟踪:-

    05-18 16:26:50.946: W/dalvikvm(381): Unable to resolve superclass of Lgeopoint/ns/GeoPointActivity; (22)
    05-18 16:26:51.026: W/dalvikvm(381): Link of class 'Lgeopoint/ns/GeoPointActivity;' failed
    05-18 16:26:51.066: D/AndroidRuntime(381): Shutting down VM
    05-18 16:26:51.066: W/dalvikvm(381): threadid=1: thread exiting with uncaught exception (group=0x40015560)
    05-18 16:26:51.206: E/AndroidRuntime(381): FATAL EXCEPTION: main
    05-18 16:26:51.206: E/AndroidRuntime(381): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{geopoint.ns/geopoint.ns.GeoPointActivity}: java.lang.ClassNotFoundException: geopoint.ns.GeoPointActivity in loader dalvik.system.PathClassLoader[/data/app/geopoint.ns-2.apk]
    05-18 16:26:51.206: E/AndroidRuntime(381):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
    05-18 16:26:51.206: E/AndroidRuntime(381):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    05-18 16:26:51.206: E/AndroidRuntime(381):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    05-18 16:26:51.206: E/AndroidRuntime(381):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    05-18 16:26:51.206: E/AndroidRuntime(381):  at android.os.Handler.dispatchMessage(Handler.java:99)
    05-18 16:26:51.206: E/AndroidRuntime(381):  at android.os.Looper.loop(Looper.java:130)
    05-18 16:26:51.206: E/AndroidRuntime(381):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    05-18 16:26:51.206: E/AndroidRuntime(381):  at java.lang.reflect.Method.invokeNative(Native Method)
    05-18 16:26:51.206: E/AndroidRuntime(381):  at java.lang.reflect.Method.invoke(Method.java:507)
    05-18 16:26:51.206: E/AndroidRuntime(381):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    05-18 16:26:51.206: E/AndroidRuntime(381):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    05-18 16:26:51.206: E/AndroidRuntime(381):  at dalvik.system.NativeStart.main(Native Method)
    05-18 16:26:51.206: E/AndroidRuntime(381): Caused by: java.lang.ClassNotFoundException: geopoint.ns.GeoPointActivity in loader dalvik.system.PathClassLoader[/data/app/geopoint.ns-2.apk]
    05-18 16:26:51.206: E/AndroidRuntime(381):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
    05-18 16:26:51.206: E/AndroidRuntime(381):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
    05-18 16:26:51.206: E/AndroidRuntime(381):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    05-18 16:26:51.206: E/AndroidRuntime(381):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
    05-18 16:26:51.206: E/AndroidRuntime(381):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
    05-18 16:26:51.206: E/AndroidRuntime(381):  ... 11 more
    05-18 16:26:53.815: I/Process(381): Sending signal. PID: 381 SIG: 9

是我在做任何逻辑错误吗?

更新(AndroidManifest.xml):

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

        <uses-sdk android:minSdkVersion="10" />
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:name=".GeoPointActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

布局xml文件: http: //pastebin.com/q176i5d2

4

2 回答 2

2

您没有将 Google 地图库添加到清单中的项目中,

在应用程序标签下添加此代码

<uses-library android:required="true" android:name="com.google.android.maps" />

并确保您的项目目标 SDK 是 Google API,而不是普通的 SDK。

祝你好运..

于 2012-05-19T11:31:51.447 回答
0

您是否在 Android 清单文件中添加了活动?

于 2012-05-17T21:53:20.027 回答