我研究了这个Nullpointer Exception。谷歌搜索后,我发现其中大部分是由于对UI元素的空引用。但我的没有。我只是无法得到空指针的原因。我在运行2.3.7的Android手机上直接测试了它。是的,我在清单文件中添加了权限。
package com.GodTM.projectshoppers;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
public class HomeActivity extends Activity {
private ImageButton get;
private ImageView quit,settings;
private LocationManager locMan;
public LocationListener LocLis;
public Location currLoc;
private double[] passloc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
get=(ImageButton)findViewById(R.id.homeGetButton);
settings = (ImageView)findViewById(R.id.homeSettingsButton);
quit = (ImageView)findViewById(R.id.homeExitButton);
Criteria crit=new Criteria();
locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LocLis = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(location != null){
currLoc.setLatitude(location.getLatitude());
currLoc.setLongitude(location.getLongitude());
}
}
};
//Criteria crit = new Criteria();
if(currLoc !=null){
passloc[0] = currLoc.getLatitude();
passloc[1] = currLoc.getLongitude();
Toast.makeText(getApplicationContext(), "Lat:" + currLoc.getLatitude() + "\nLong:" + currLoc.getLongitude(), Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Not Available", Toast.LENGTH_SHORT).show();
}
get.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(currLoc!=null){
Intent i = new Intent(HomeActivity.this, Screen2.class);
i.putExtra("Loc", passloc);
startActivity(i);
}
else{
Toast.makeText(getApplicationContext(), "Location Not Available", Toast.LENGTH_SHORT).show();
}
}
});
settings.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Intent j = new Intent(this, screen4.class);
// startActivity(j);
}
});
quit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
@Override
public void onResume(){
super.onResume();
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER , 0, 0, LocLis);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
}
答案 0 :(得分:3)
currLoc
永远不会被初始化。由于地理位置和currLoc
都是Location
,您可以更改
if(location != null){
currLoc.setLatitude(location.getLatitude());
currLoc.setLongitude(location.getLongitude());
}
带
currLoc = location;
答案 1 :(得分:0)
您没有初始化passLoc
。并且您在不保存位置的情况下致电getLastKnownLocation
。