当我运行应用程序时,它会发送消息,遗憾的是loca已停止并关闭应用程序
Java编码
package com.example.loca;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements LocationListener {
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
TextView txtLat;
String lat, place1, currentlocation, btn_text;
String provider;
protected String latitude,longitude;
protected boolean gps_enabled,network_enabled;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Toast.makeText(getApplicationContext(), currentlocation , Toast.LENGTH_LONG).show();
}
@Override
public void onLocationChanged(Location location) {
Toast.makeText(getApplicationContext(), currentlocation , Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
}
@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
}
}
AND LOGCAT
05-14 01:20:39.210: D/AndroidRuntime(1177): Shutting down VM
05-14 01:20:39.210: W/dalvikvm(1177): threadid=1: thread exiting with uncaught exception (group=0xb3aebb90)
05-14 01:20:39.230: E/AndroidRuntime(1177): FATAL EXCEPTION: main
05-14 01:20:39.230: E/AndroidRuntime(1177): Process: com.example.loca, PID: 1177
05-14 01:20:39.230: E/AndroidRuntime(1177): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loca/com.example.loca.MainActivity}: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.app.ActivityThread.access$700(ActivityThread.java:135)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.os.Handler.dispatchMessage(Handler.java:102)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.os.Looper.loop(Looper.java:137)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.app.ActivityThread.main(ActivityThread.java:4998)
05-14 01:20:39.230: E/AndroidRuntime(1177): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 01:20:39.230: E/AndroidRuntime(1177): at java.lang.reflect.Method.invoke(Method.java:515)
05-14 01:20:39.230: E/AndroidRuntime(1177): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
05-14 01:20:39.230: E/AndroidRuntime(1177): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
05-14 01:20:39.230: E/AndroidRuntime(1177): at dalvik.system.NativeStart.main(Native Method)
05-14 01:20:39.230: E/AndroidRuntime(1177): Caused by: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.os.Parcel.readException(Parcel.java:1461)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.os.Parcel.readException(Parcel.java:1415)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:540)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:860)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:454)
05-14 01:20:39.230: E/AndroidRuntime(1177): at com.example.loca.MainActivity.onCreate(MainActivity.java:34)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.app.Activity.performCreate(Activity.java:5243)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-14 01:20:39.230: E/AndroidRuntime(1177): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
05-14 01:20:39.230: E/AndroidRuntime(1177): ... 11 more
05-14 01:20:47.040: I/Process(1177): Sending signal. PID: 1177 SIG: 9
答案 0 :(得分:2)
您需要请求FINE_LOCATION权限,就像例外情况一样。
答案 1 :(得分:2)
你的logcat清楚地说了
引起:java.lang.SecurityException:" gps"位置提供者 需要ACCESS_FINE_LOCATION权限。 05-14 01:20:39.230: E / AndroidRuntime(1177):
您需要在permission
文件
manifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
答案 2 :(得分:1)
日志说明了一切:
location provider requires ACCESS_FINE_LOCATION permission.
在AndroidManifest.xml中添加此权限,您的应用程序将正常运行。