0

在这里,我想首先检查 gps 是否启用,如果 gps 启用,然后在谷歌地图上显示用户位置,检查 gps 是否启用,但它没有打开谷歌地图的类,它们是 MainActivity 类中的一些问题。我已经添加了 google play 服务库..以及 jar 文件..google play 和支持 v4 ...这是我的代码

gpsactivator.java

package com.example.googlemaps;



import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.maps.MapView;
import com.google.android.maps.MapController;

public class Gpsactivator extends Activity {

    Boolean enable = false;
    LocationManager locationManager;
    int x = 0;
    private MapView mapView;
    private MapController mapController;

    //Provider gpsprovider;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
         String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    //  gpsprovider=LocationManager.GPS_PROVIDER;
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        Toast.makeText(this, "GPS is Enabled in your devide", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(this,MainActivity.class));
        }else
        {

            showGPSDisabledAlertToUser();
            new ultimate().execute("");
            //Toast.makeText(this, "GPS is now Enabled in your devide", Toast.LENGTH_SHORT).show();


//          
    //  ProgressDialog dialog = ProgressDialog.show(Gpsactivator.this, "", "Loading. Please wait...", true);
//          while(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
//      {
//          Log.e("TAG",status.toString() );
//          if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
//          {
//              startActivity(new Intent(this,MainActivity.class));
//      //      dialog.dismiss();
//              break;
//          }
//      }
        }

    }

    private void showGPSDisabledAlertToUser(){
    //  Toast.makeText(this, "Enabled your Gps and restart your app", Toast.LENGTH_SHORT).show();

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?")
        .setCancelable(false)
        .setPositiveButton("Goto Settings Page To Enable GPS",
        new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
        Intent callGPSSettingIntent = new Intent(
        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(callGPSSettingIntent);
        }
        }
        );
        alertDialogBuilder.setNegativeButton("Cancel",
        new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
        dialog.cancel();
        }
        });
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();

        }


    private class ultimate extends AsyncTask<String,Void ,Integer> {

        @Override
        protected Integer doInBackground(String... params) {
            // TODO Auto-generated method stub
            Boolean status=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            Log.e("TAAAG",status.toString()+"1" );
            while(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
            {
                Log.e("TAG",status.toString() );
                if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
                {

                    startActivity(new Intent(Gpsactivator.this,MainActivity.class));
            //      dialog.dismiss();
                    break;
                }
            }
      return null;
        }



      }
@Override   
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();


    }





}

Mainactivity.java

package com.example.googlemaps;

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

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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.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;
import com.google.android.maps.Overlay;

//public class MainActivity extends FragmentActivity {
//  
//  
//  private GoogleMap mMap;
//  
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
//    
//      //  mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
//        setUpMapIfNeeded();
//  }
//    private void setUpMapIfNeeded() {
//        // Do a null check to confirm that we have not already instantiated the map.
//        if (mMap == null) {
//          mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
//            // Check if we were successful in obtaining the map.
//            if (mMap != null) {
//                // The Map is verified. It is now safe to manipulate the map.
//
//            }
//        }
//    }
//}

public class MainActivity extends MapActivity {
    private MapView mapView;
    private MapController mapController;

    private LocationManager locationManager;
    private LocationListener locationListener;

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        locationListener = new GPSLocationListener();

        locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            0, 
            0, 
            locationListener);

        mapView = (MapView) findViewById(R.id.mapView);

        // enable Street view by default
        mapView.setStreetView(true);

        // enable to show Satellite view
        // mapView.setSatellite(true);

        // enable to show Traffic on map
        // mapView.setTraffic(true);
        mapView.setBuiltInZoomControls(true);

        mapController = mapView.getController();
        mapController.setZoom(16); 
    }

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

    private class GPSLocationListener implements LocationListener 
    {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                GeoPoint point = new GeoPoint(
                        (int) (location.getLatitude() * 1E6), 
                        (int) (location.getLongitude() * 1E6));

                /* Toast.makeText(getBaseContext(), 
                        "Latitude: " + location.getLatitude() + 
                        " Longitude: " + location.getLongitude(), 
                        Toast.LENGTH_SHORT).show();*/

                mapController.animateTo(point);
                mapController.setZoom(16);

                // add marker
                MapOverlay mapOverlay = new MapOverlay();
                mapOverlay.setPointToDraw(point);
                List<Overlay> listOfOverlays = mapView.getOverlays();
                listOfOverlays.clear();
                listOfOverlays.add(mapOverlay);

                String address = ConvertPointToLocation(point);
                Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();

                mapView.invalidate();
            }
        }

        public String ConvertPointToLocation(GeoPoint point) {   
            String address = "";
            Geocoder geoCoder = new Geocoder(
                    getBaseContext(), Locale.getDefault());
            try {
                List<Address> addresses = geoCoder.getFromLocation(
                    point.getLatitudeE6()  / 1E6, 
                    point.getLongitudeE6() / 1E6, 1);

                if (addresses.size() > 0) {
                    for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
                        address += addresses.get(0).getAddressLine(index) + " ";
                }
            }
            catch (IOException e) {                
                e.printStackTrace();
            }   

            return address;
        } 

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

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

    class MapOverlay extends Overlay
    {
        private GeoPoint pointToDraw;

        public void setPointToDraw(GeoPoint point) {
            pointToDraw = point;
        }

        public GeoPoint getPointToDraw() {
            return pointToDraw;
        }

        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
            super.draw(canvas, mapView, shadow);                   

            // convert point to pixels
            Point screenPts = new Point();
            mapView.getProjection().toPixels(pointToDraw, screenPts);

            // add marker
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.red);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null); // 24 is the height of image        
            return true;
        }
    } 
}

AndroidManifestfile.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <permission
        android:name="com.example.googlemaps.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.example.googlemaps.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <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.googlemaps.Gpsactivator"
            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=".MainActivity"
            android:label="MainActivity" 
            >

        </activity>



 <uses-library android:name="com.google.android.maps" />       <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCGqPvJLn_RSvl_eGZaHpYSG5MPbgLo8_4" />
    </application>

</manifest> 

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <com.google.android.maps.MapView 
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"

        />
</LinearLayout>

日志文件

07-26 17:20:18.145: W/dalvikvm(15108): Unable to resolve superclass of Lcom/example/googlemaps/MainActivity; (1197)
07-26 17:20:18.145: W/dalvikvm(15108): Link of class 'Lcom/example/googlemaps/MainActivity;' failed
07-26 17:20:18.145: E/dalvikvm(15108): Could not find class 'com.example.googlemaps.MainActivity', referenced from method com.example.googlemaps.Gpsactivator.onCreate
07-26 17:20:18.145: W/dalvikvm(15108): VFY: unable to resolve const-class 591 (Lcom/example/googlemaps/MainActivity;) in Lcom/example/googlemaps/Gpsactivator;
07-26 17:20:18.145: D/dalvikvm(15108): VFY: replacing opcode 0x1c at 0x0033
07-26 17:20:18.145: D/dalvikvm(15108): VFY: dead code 0x0035-003a in Lcom/example/googlemaps/Gpsactivator;.onCreate (Landroid/os/Bundle;)V
07-26 17:20:18.145: I/ApplicationPackageManager(15108): cscCountry is not German : INS
07-26 17:20:19.036: D/AndroidRuntime(15108): Shutting down VM
07-26 17:20:19.036: W/dalvikvm(15108): threadid=1: thread exiting with uncaught exception (group=0x40018578)
07-26 17:20:19.731: E/AndroidRuntime(15108): FATAL EXCEPTION: main
07-26 17:20:19.731: E/AndroidRuntime(15108): java.lang.NoClassDefFoundError: com.example.googlemaps.MainActivity
07-26 17:20:19.731: E/AndroidRuntime(15108):    at com.example.googlemaps.Gpsactivator.onCreate(Gpsactivator.java:37)
07-26 17:20:19.731: E/AndroidRuntime(15108):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-26 17:20:19.731: E/AndroidRuntime(15108):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
07-26 17:20:19.731: E/AndroidRuntime(15108):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
07-26 17:20:19.731: E/AndroidRuntime(15108):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-26 17:20:19.731: E/AndroidRuntime(15108):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
07-26 17:20:19.731: E/AndroidRuntime(15108):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 17:20:19.731: E/AndroidRuntime(15108):    at android.os.Looper.loop(Looper.java:130)
07-26 17:20:19.731: E/AndroidRuntime(15108):    at android.app.ActivityThread.main(ActivityThread.java:3687)
07-26 17:20:19.731: E/AndroidRuntime(15108):    at java.lang.reflect.Method.invokeNative(Native Method)
07-26 17:20:19.731: E/AndroidRuntime(15108):    at java.lang.reflect.Method.invoke(Method.java:507)
07-26 17:20:19.731: E/AndroidRuntime(15108):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-26 17:20:19.731: E/AndroidRuntime(15108):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-26 17:20:19.731: E/AndroidRuntime(15108):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

改变

public class MainActivity extends Activity

public class MainActivity extends FragmentActivity
于 2013-07-30T17:40:10.900 回答