正如标题所说,当我想调用requestLocationUpdates时,我的应用程序崩溃了。 现在,我只想在更新位置时给出一条日志消息,看看发生的速度有多快。
MainActivity
private TextView textViewCurrentLocation;
LocationManager mLocationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewCurrentLocation = (TextView) findViewById(R.id.textViewLocation);
mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener mlocationListener = new LocationListener() {
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
Log.i("MainActivity", "onLocationChanged");
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
};
// Register the listener with the Location Manager to receive location
// updates
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
mlocationListener);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
mlocationListener);
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="at.itpro.locationreader"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
修改
我添加了所有这些权限 - &gt;没有变化
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />
<uses-permission android:name="android.permission.INTERNET" />
Logcat输出
09-17 12:50:00.518: E/AndroidRuntime(23726): FATAL EXCEPTION: main
09-17 12:50:00.518: E/AndroidRuntime(23726): java.lang.RuntimeException: Unable to start activity ComponentInfo{at.itpro.locationreader/at.itpro.locationreader.MainActivity}: java.lang.IllegalArgumentException: provider=network
09-17 12:50:00.518: E/AndroidRuntime(23726): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2108)
09-17 12:50:00.518: E/AndroidRuntime(23726): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2133)
09-17 12:50:00.518: E/AndroidRuntime(23726): at android.app.ActivityThread.access$600(ActivityThread.java:142)
09-17 12:50:00.518: E/AndroidRuntime(23726): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-17 12:50:00.518: E/AndroidRuntime(23726): at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 12:50:00.518: E/AndroidRuntime(23726): at android.os.Looper.loop(Looper.java:137)
09-17 12:50:00.518: E/AndroidRuntime(23726): at android.app.ActivityThread.main(ActivityThread.java:4881)
09-17 12:50:00.518: E/AndroidRuntime(23726): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 12:50:00.518: E/AndroidRuntime(23726): at java.lang.reflect.Method.invoke(Method.java:511)
09-17 12:50:00.518: E/AndroidRuntime(23726): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
09-17 12:50:00.518: E/AndroidRuntime(23726): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
09-17 12:50:00.518: E/AndroidRuntime(23726): at dalvik.system.NativeStart.main(Native Method)
09-17 12:50:00.518: E/AndroidRuntime(23726): Caused by: java.lang.IllegalArgumentException: provider=network
09-17 12:50:00.518: E/AndroidRuntime(23726): at android.os.Parcel.readException(Parcel.java:1429)
09-17 12:50:00.518: E/AndroidRuntime(23726): at android.os.Parcel.readException(Parcel.java:1379)
09-17 12:50:00.518: E/AndroidRuntime(23726): at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:659)
09-17 12:50:00.518: E/AndroidRuntime(23726): at android.location.LocationManager._requestLocationUpdates(LocationManager.java:690)
09-17 12:50:00.518: E/AndroidRuntime(23726): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:512)
09-17 12:50:00.518: E/AndroidRuntime(23726): at com.company .locationreader.MainActivity.onCreate(MainActivity.java:48)
09-17 12:50:00.518: E/AndroidRuntime(23726): at android.app.Activity.performCreate(Activity.java:5042)
09-17 12:50:00.518: E/AndroidRuntime(23726): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
09-17 12:50:00.518: E/AndroidRuntime(23726): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2072)
09-17 12:50:00.518: E/AndroidRuntime(23726): ... 11 more
答案 0 :(得分:1)
更改我的搜索字词后,我自己找到了答案。
如此处所述StackOverflow - Caused by: java.lang.IllegalArgumentException: provider=network
某些设备不支持NETWORK_PROVIDER,因此您必须先检查是否已启用networkprovider:
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
答案 1 :(得分:0)
我确定你已经忘记了一些权限。请考虑将这些权限添加到Android Manifest
。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />
<uses-permission android:name="android.permission.INTERNET" />
答案 2 :(得分:0)
在应用程序清单中添加这些权限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />