我真正需要做的是当我按下位置按钮是为了获取我当前的位置并将其显示为编辑文本中没有地图的地址..但它一直给我NULL指针EXCEPTION ...为什么??? < / p>
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)
答案 0 :(得分: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();
}
}
答案 1 :(得分:0)
要获取LocationManager并要求更新位置,请在oncreate();
中使用这些行 mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, mLocationListener);
然后使用它来获取设备的当前latlong ..
private final LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
//write your code here
}
};
也不要忘记添加所需的权限..