我正在做的是尝试获得基本的GPS工作,logcat说:
03-04 17:06:37.052: E/AndroidRuntime(278): FATAL EXCEPTION: main
03-04 17:06:37.052: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to start activity ComponentInfo{Weather.app/Weather.app.WeatherAppActivity}: java.lang.IllegalArgumentException: listener==null
03-04 17:06:37.052: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-04 17:06:37.052: E/AndroidRuntime(278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-04 17:06:37.052: E/AndroidRuntime(278): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-04 17:06:37.052: E/AndroidRuntime(278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-04 17:06:37.052: E/AndroidRuntime(278): at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 17:06:37.052: E/AndroidRuntime(278): at android.os.Looper.loop(Looper.java:123)
03-04 17:06:37.052: E/AndroidRuntime(278): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-04 17:06:37.052: E/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 17:06:37.052: E/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521)
03-04 17:06:37.052: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-04 17:06:37.052: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-04 17:06:37.052: E/AndroidRuntime(278): at dalvik.system.NativeStart.main(Native Method)
03-04 17:06:37.052: E/AndroidRuntime(278): Caused by: java.lang.IllegalArgumentException: listener==null
03-04 17:06:37.052: E/AndroidRuntime(278): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:628)
03-04 17:06:37.052: E/AndroidRuntime(278): at Weather.app.WeatherAppActivity.onCreate(WeatherAppActivity.java:36)
03-04 17:06:37.052: E/AndroidRuntime(278): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-04 17:06:37.052: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-04 17:06:37.052: E/AndroidRuntime(278): ... 11 more
public class WeatherAppActivity extends Activity{
/** Called when the activity is first created. */
//private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
//private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
static final String tag = "Main"; // for Log
protected LocationManager locationManager;
protected LocationListener MyLocationListener;
protected Button findButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
findButton = new Button(this);
ll.addView(findButton);
setContentView(ll);
locationListener = new MyLocationListener();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, locationListener
);
findButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showCurrentLocation();
}
});
}
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(WeatherAppActivity.this, message,
Toast.LENGTH_LONG).show();
}
final class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(WeatherAppActivity.this, message, Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
/* This is called when the GPS status alters */
switch (status) {
case LocationProvider.OUT_OF_SERVICE:
Log.v(tag, "Status Changed: Out of Service");
Toast.makeText(WeatherAppActivity.this, "Status Changed: Out of Service",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.v(tag, "Status Changed: Temporarily Unavailable");
Toast.makeText(WeatherAppActivity.this, "Status Changed: Temporarily Unavailable",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.AVAILABLE:
Log.v(tag, "Status Changed: Available");
Toast.makeText(WeatherAppActivity.this, "Status Changed: Available",
Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(WeatherAppActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(WeatherAppActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
}
}
这也在我的清单中:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />
答案 0 :(得分:0)
试试这个变种。我刚刚在手机上测试它没有崩溃。 权限似乎没问题。
public class WeatherAppActivity extends Activity{
/** Called when the activity is first created. */
//private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
//private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
static final String tag = "Main"; // for Log
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Button findButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
findButton = new Button(this);
findButton.setText("findMeh");
ll.addView(findButton);
setContentView(ll);
locationListener = new MyLocationListener();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, locationListener
);
findButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showCurrentLocation();
}
});
}
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(WeatherAppActivity.this, message,
Toast.LENGTH_LONG).show();
}
}
class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(WeatherAppActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
/* This is called when the GPS status alters */
switch (status) {
case LocationProvider.OUT_OF_SERVICE:
Log.v(tag, "Status Changed: Out of Service");
Toast.makeText(WeatherAppActivity.this, "Status Changed: Out of Service",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.v(tag, "Status Changed: Temporarily Unavailable");
Toast.makeText(WeatherAppActivity.this, "Status Changed: Temporarily Unavailable",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.AVAILABLE:
Log.v(tag, "Status Changed: Available");
Toast.makeText(WeatherAppActivity.this, "Status Changed: Available",
Toast.LENGTH_SHORT).show();
break;
}
}
public void onProviderDisabled(String provider) {
Toast.makeText(WeatherAppActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String provider) {
Toast.makeText(WeatherAppActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
}