我正在制作GPS应用程序,您可以通过按下按钮获取GPS坐标。正如标题中所述,当我点击按钮时它会关闭。
我是Android开发的新手,并且远离我对Java的一点知识并且遵循一些Android教程。
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements LocationListener {
private TextView startLatitudeField;
private TextView startLongitudeField;
private TextView endLatitudeField;
private TextView endLongitudeField;
private LocationManager locationManager;
private LocationListener locationListener;
private String provider;
double startLat = 0;
double startLng = 0;
double endLat = 0;
double endLng = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//Setting TextViews for the coordinates
startLatitudeField = (TextView) findViewById(R.id.TextView02);
startLongitudeField = (TextView) findViewById(R.id.TextView04);
endLatitudeField = (TextView) findViewById(R.id.textView06);
endLongitudeField = (TextView) findViewById(R.id.textView08);
//Button
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location location = locationManager.getLastKnownLocation(provider);
if (startLat != 0) {
onLocationChanged(location);
} else {
onLocationChanged(location);
Button button = (Button) findViewById(R.id.button1);
button.setText("Get me home");
}
}
});
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
if (startLat == 0) {
startLat = (double) (location.getLatitude());
startLng = (double) (location.getLongitude());
startLatitudeField.setText(String.valueOf(startLat));
startLongitudeField.setText(String.valueOf(startLng));
}
else {
endLat = (double) (location.getLatitude());
endLng = (double) (location.getLongitude());
endLatitudeField.setText(String.valueOf(endLat));
endLongitudeField.setText(String.valueOf(endLng));
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
以下是我的错误日志:
02-24 12:57:55.580: I/Process(4655): Sending signal. PID: 4655 SIG: 9
02-24 12:58:09.664: D/AndroidRuntime(4772): Shutting down VM
02-24 12:58:09.664: W/dalvikvm(4772): threadid=1: thread exiting with uncaught exception (group=0x41544ba8)
02-24 12:58:09.664: E/AndroidRuntime(4772): FATAL EXCEPTION: main
02-24 12:58:09.664: E/AndroidRuntime(4772): Process: com.example.getmehome, PID: 4772
02-24 12:58:09.664: E/AndroidRuntime(4772): java.lang.IllegalArgumentException: invalid listener: null
02-24 12:58:09.664: E/AndroidRuntime(4772): at android.location.LocationManager.checkListener(LocationManager.java:1635)
02-24 12:58:09.664: E/AndroidRuntime(4772): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:450)
02-24 12:58:09.664: E/AndroidRuntime(4772): at com.example.getmehome.MainActivity$1.onClick(MainActivity.java:57)
02-24 12:58:09.664: E/AndroidRuntime(4772): at android.view.View.performClick(View.java:4438)
02-24 12:58:09.664: E/AndroidRuntime(4772): at android.view.View$PerformClick.run(View.java:18422)
02-24 12:58:09.664: E/AndroidRuntime(4772): at android.os.Handler.handleCallback(Handler.java:733)
02-24 12:58:09.664: E/AndroidRuntime(4772): at android.os.Handler.dispatchMessage(Handler.java:95)
02-24 12:58:09.664: E/AndroidRuntime(4772): at android.os.Looper.loop(Looper.java:136)
02-24 12:58:09.664: E/AndroidRuntime(4772): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-24 12:58:09.664: E/AndroidRuntime(4772): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 12:58:09.664: E/AndroidRuntime(4772): at java.lang.reflect.Method.invoke(Method.java:515)
02-24 12:58:09.664: E/AndroidRuntime(4772): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-24 12:58:09.664: E/AndroidRuntime(4772): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-24 12:58:09.664: E/AndroidRuntime(4772): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您的Activity
必须implements LocationListener
和implements all the methods
。
更新:您应将提供商添加到位置,例如:
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
在此您必须使用GPS_PROVIDER
以便设置Accuracy
criteria.setAccuracy(Criteria.ACCURACY_FINE);
如果您使用NETWORK_PROVIDER
,请设置Accuracy
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
答案 1 :(得分:1)
您忘记初始化provider
& locationListener
变量,这就是它显示IllegalArgumentException: invalid listener: null
我已经编辑了您的代码,现在它正在我的设备中运行,
public class MainActivity extends Activity
{
private TextView startLatitudeField;
private TextView startLongitudeField;
private TextView endLatitudeField;
private TextView endLongitudeField;
private LocationManager locationManager;
private LocationListener locationListener;
private String provider;
double startLat = 0;
double startLng = 0;
double endLat = 0;
double endLng = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//Setting TextViews for the coordinates
startLatitudeField = (TextView) findViewById(R.id.txtView1);
startLongitudeField = (TextView) findViewById(R.id.txtView2);
endLatitudeField = (TextView) findViewById(R.id.txtView3);
endLongitudeField = (TextView) findViewById(R.id.txtView4);
//Button
Button button = (Button) findViewById(R.id.cmdButton);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener ( );
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (startLat != 0)
{
locationListener.onLocationChanged(location);
} else
{
locationListener.onLocationChanged(location);
Button button = (Button) findViewById(R.id.cmdButton);
button.setText("Get me home");
}
}
});
};
private class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location)
{
// TODO Auto-generated method stub
if (startLat == 0) {
startLat = (double) (location.getLatitude());
startLng = (double) (location.getLongitude());
startLatitudeField.setText(String.valueOf(startLat));
startLongitudeField.setText(String.valueOf(startLng));
}
else {
endLat = (double) (location.getLatitude());
endLng = (double) (location.getLongitude());
endLatitudeField.setText(String.valueOf(endLat));
endLongitudeField.setText(String.valueOf(endLng));
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}