2014-05-18 103 views
-1

我真正需要做的是當我按位置按鈕是爲了獲得我當前的位置並顯示它像一個地址在編輯文本沒有地圖..但它保持給我NULL POINTER EXCEPTION ..爲什麼?爲什麼我不能通過按鈕獲得我的當前位置

public class PersonalInfo extends Activity{ 
    private GoogleMap googleMap; 
    Geocoder geocoder; 
    EditText addressnew; 
    GPSTracker gps; 
    @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
      setContentView(R.layout.personal_info); 
      EditText name=(EditText)findViewById(R.id.edittext1); 
      EditText mobile=(EditText)findViewById(R.id.edittext2); 
      addressnew=(EditText)findViewById(R.id.editText3); 
      Button location=(Button)findViewById(R.id.button1); 
      Button send=(Button)findViewById(R.id.button2); 


      location.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 


        gps = new GPSTracker(PersonalInfo.this); 

        // check if GPS enabled 
        if (gps.canGetLocation()) { 
        //enable my location 
        googleMap.setMyLocationEnabled(true); 
        //get location object 
        LocationManager locationManager=(LocationManager)getSystemService(LOCATION_SERVICE); 
        //to retrive provider 
        Criteria criteria = new Criteria(); 

        String provider = locationManager.getBestProvider(criteria, true); 
        //get current location 

        Location mylocation=locationManager.getLastKnownLocation(provider); 
        //set map type 
        //googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
        double latitude = mylocation.getLatitude(); 
        double longitude = mylocation.getLongitude(); 
        LatLng latlng=new LatLng(latitude,longitude); 


        Toast.makeText(PersonalInfo.this, latitude+" "+longitude, Toast.LENGTH_LONG).show(); 

        List<Address> addresses; 
        geocoder = new Geocoder(PersonalInfo.this, Locale.getDefault()); 
        try{ 
         addresses = geocoder.getFromLocation(latitude, longitude, 1); 
         String address = addresses.get(0).getAddressLine(0); 
         String city = addresses.get(0).getAddressLine(1); 
         String country = addresses.get(0).getAddressLine(2); 
         String cities=addresses.get(0).getAdminArea(); 
         Log.e("address",addresses+""); 
         Log.e("address",address+""); 
         Log.e("city",cities+""); 
         Log.e("country",city+""); 
         addressnew.setText("your current location is: "+city+""+","+cities+""+","+address+""); 
         //String uri = "http://maps.google.com/maps?saddr=" + mylocation.getLatitude()+","+mylocation.getLongitude(); 

         //Uri path= Uri.parse(uri); 




        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        } 
        else{ 
         Toast.makeText(PersonalInfo.this, "gps is not enabled", Toast.LENGTH_LONG).show(); 
        } 

      } 
     }); 
    } 

} 

這裏是logcat的:

FATAL EXCEPTION: main 
java.lang.NullPointerException 
at adapter.PersonalInfo$1.onClick(PersonalInfo.java:60) 
at android.view.View.performClick(View.java:3530) 
at android.view.View$PerformClick.run(View.java:14227) 
at android.os.Handler.handleCallback(Handler.java:605) 
+1

發佈錯誤或logcat的 –

+0

OK我現在還請你可以刪除未投票嗎? – user3628116

+0

它在這一行googleMap.setMyLocationEnabled(true); – user3628116

回答

0

要獲取LocationManager並要求位置更新,請在oncreate()中使用這些行;

mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
      0, mLocationListener); 

,然後用它來獲取設備的當前經緯度..

private final LocationListener mLocationListener = new LocationListener() { 
    @Override 
    public void onLocationChanged(final Location location) { 
     //write your code here 
    } 
}; 

也不要忘記添加所需的權限..

0

請使用下面的代碼。

package com.example.loation; 

import java.util.List; 
import java.util.Locale; 

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.location.Address; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 

public class GettingLocation extends Activity { 

    private Double dbllatitude, dbllongitude; 
    private LocationManager myLocationManager; 
    private LocationListener myLocationListener; 
    Geocoder geocoder; 
    private LocationControl locationControlTask; 
    private boolean hasLocation = false; 
    protected Activity Activity; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     this.Activity = this; 
     GetLocation(); 
    } 

    private void GetLocation() { 

     myLocationManager = (LocationManager) Activity.getSystemService(Context.LOCATION_SERVICE); 
     myLocationListener = new MyLocationListener(); 
     myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationListener); 
     // getlon.setText("Stop"); 
     if (!myLocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) { 
      displayalert(); 
     } else { 
      locationControlTask = new LocationControl(); 
      locationControlTask.execute(); 
     } 
    } 

    public class MyLocationListener implements LocationListener { 
     public void onLocationChanged(Location argLocation) { 
      if (argLocation != null) { 
       dbllatitude = argLocation.getLatitude(); 
       dbllongitude = argLocation.getLongitude(); 
       hasLocation = true; 
      } 
     } 

     public void onProviderDisabled(String provider) {} 

     public void onProviderEnabled(String provider) {} 

     public void onStatusChanged(String provider, int status, Bundle arg2) {} 
    }; 

    @SuppressLint("NewApi") 
    private class LocationControl extends AsyncTask<Activity, Void, Void> { 
     private final ProgressDialog dialog = new ProgressDialog(Activity); 

     protected void onPreExecute() { 
      this.dialog.setMessage("Getting Location..........."); 
      this.dialog.setButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.dismiss(); 
        hasLocation = true; 
       } 
      }); 
      this.dialog.show(); 
     } 

     protected Void doInBackground(Activity... params) { 
      while (!hasLocation) { 
       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
      ; 
      return null; 
     } 

     protected void onPostExecute(final Void unused) { 
      if (this.dialog.isShowing()) { 
       this.dialog.dismiss(); 
      } 

      if (dbllatitude != null && dbllongitude != null && dbllatitude != 0.0 && dbllongitude != 0.0) { 
       try { 
        // Write your address code here 
        List<Address> addresses; 
        geocoder = new Geocoder(GettingLocation.this, Locale.getDefault()); 
        addresses = geocoder.getFromLocation(dbllatitude, dbllongitude, 1); 
        String address = addresses.get(0).getAddressLine(0); 
        String city = addresses.get(0).getAddressLine(1); 
        String country = addresses.get(0).getAddressLine(2); 
        String cities = addresses.get(0).getAdminArea(); 
        Log.e("address", addresses + ""); 
        Log.e("address", address + ""); 
        Log.e("city", cities + ""); 
        Log.e("country", city + ""); 

       } catch (Exception e) { 
        System.out.println("Error:::::::" + e.getMessage()); 
       } 
      } else { 
       displayalert1(); 
      } 
     } 

    } 

    public void displayalert1() { 
     AlertDialog.Builder builder = new AlertDialog.Builder(Activity); 
     builder.setMessage("Location not found. Do you want to try again?").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       hasLocation = false; 
       locationControlTask = new LocationControl(); 
       locationControlTask.execute(); 
      } 
     }); 
     builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 

      } 
     }); 
     builder.show(); 
    } 

    private void displayalert() { 
     AlertDialog.Builder builder = new AlertDialog.Builder(Activity); 
     builder.setMessage("GPS is turned off ,Please Enable GPS").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 

      } 
     }); 
     builder.show(); 

    } 

} 
+0

什麼是locationControl ..它不能被解析爲一個類型 – user3628116

相關問題