GPS 位置情報を取得しようとしましたが、次のエラーが表示されます
09-21 16:01:57.316: E/AndroidRuntime(8775): FATAL EXCEPTION: main
09-21 16:01:57.316: E/AndroidRuntime(8775): java.lang.NullPointerException
"09-21 16:01:57.316: E/AndroidRuntime(8775): at com.mgrs.converter.pro.Geocoord.getLocation(Geocoord.java:26)"
私はそれを修正しようとしましたが、修正する方法がわかりません。
助けてください
MainActivity.java
public class MainActivity extends Activity {
TextView site1_lat, site1_lon, site2_lat, site2_lon, distance_kilo, distance_miles, azimuth1_content, azimuth2_content;
TextView current_lat, current_lon, current_mgrs_result;
Variables vari;
//for geoCoord
//Context mContext;
LocationManager mLocMan;
String mProvider;
//Location location;
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//for geoCoord
//mLocMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//mProvider = mLocMan.getBestProvider(new Criteria(), true);
//LocationListener mListener = new Geocoord();
//mLocMan.requestLocationUpdates(mProvider, 6000, 10, mListener);
//
//Button 0
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//1st site
EditText mgrs_site1 = (EditText)findViewById(R.id.editText1);
String site1 = mgrs_site1.getText().toString().toUpperCase();
Converter converter = new Converter();
Geocoord geocoord = new Geocoord();
geocoord.getLocation(context);
converter.MGRStoUTM(site1);
///get Lat, Longitude
site1_lat = (TextView)findViewById(R.id.site1_lat);
site1_lon = (TextView)findViewById(R.id.site1_lon);
site1_lat.setText(String.format("%6f", converter.getinitLat()));
site1_lon.setText(String.format("%6f", converter.getinitLon()));
//2nd site
EditText mgrs_site2 = (EditText)findViewById(R.id.editText2);
String site2 = mgrs_site2.getText().toString().toUpperCase();
converter.MGRStoUTM(site2);
///get Lat, Longitude
site2_lat = (TextView)findViewById(R.id.site2_lat);
site2_lon = (TextView)findViewById(R.id.site2_lon);
site2_lat.setText(String.format("%6f", converter.getinitLat()));
site2_lon.setText(String.format("%6f", converter.getinitLon()));
//current Lat, Longitude
current_lat = (TextView)findViewById(R.id.current_lat);
current_lon = (TextView)findViewById(R.id.current_lon);
current_lat.setText(String.format("%6f", converter.getLatitude()));
current_lon.setText(String.format("%6f", converter.getLongitude()));
//get current MGRS
String str;
converter.LLtoMGRS(converter.getLatitude(),converter.getLongitude(),10);
str=converter.getMGRS();
System.out.println("MGRS result");
System.out.println(str);
current_mgrs_result = (TextView)findViewById(R.id.current_mgrs_result);
current_mgrs_result.setText(str);
}
}); //ends button0
//Button 1
}//ends onCreate
}//ends Activity
Geocoord.java
public class Geocoord {
Variables vari;
Context mContext;
LocationManager mLocMan;
String mProvider;
double latitude;
double longitude;
public void getLocation(Context context){
mContext = context;
mLocMan = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
mProvider = mLocMan.getBestProvider(new Criteria(), true);
mLocMan.requestLocationUpdates(mProvider, 6000, 10, mListener);
}
LocationListener mListener =new LocationListener(){
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
latitude = location.getLatitude();
longitude = location.getLongitude();
setLatitude(latitude);
setLongitude(longitude);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
System.out.println("provider disabled");
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
System.out.println("provider enabled");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
System.out.println("status changed");
}
};
public void setLatitude(double gps_latitude)
{vari.gps_latitude = gps_latitude;}
public void setLongitude(double gps_longitude)
{vari.gps_longitude = gps_longitude;}
public double getLatitude()
{return vari.gps_latitude;}
public double getLongitude()
{return vari.gps_longitude;}
}