我真正需要做的是當我按位置按鈕是爲了獲得我當前的位置並顯示它像一個地址在編輯文本沒有地圖..但它保持給我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)
發佈錯誤或logcat的 –
OK我現在還請你可以刪除未投票嗎? – user3628116
它在這一行googleMap.setMyLocationEnabled(true); – user3628116