无法启动使用GPS检测android设备位置的活动实现LocationListener
我开发了一个简单的应用程序,可以检测android设备的位置,并在文本视图中显示其值 一旦应用程序开始抛出RuntimeException,它就会崩溃 下面是GPS.java无法启动使用GPS检测android设备位置的活动实现LocationListener,android,gps,Android,Gps,我开发了一个简单的应用程序,可以检测android设备的位置,并在文本视图中显示其值 一旦应用程序开始抛出RuntimeException,它就会崩溃 下面是GPS.java public class GPS extends Activity implements LocationListener { private TextView latituteField; private TextView longitudeField; private LocationManag
public class GPS extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
latituteField = (TextView) findViewById(R.id.TextView02);
longitudeField = (TextView) findViewById(R.id.TextView04);
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
Intent intent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria,false);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
Log.d("PROVIDER", "Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
}
日志如下:
10-09 16:49:35.733:E/AndroidRuntime(1616):致命异常:主
10-09 16:49:35.733:E/AndroidRuntime(1616):
java.lang.RuntimeException:无法启动活动
ComponentInfo{com.example.gps/com.example.gps.gps}:
java.lang.NullPointerException 10-09 16:49:35.733:
E/AndroidRuntime(1616):在
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-09 16:49:35.733:E/AndroidRuntime(1616):在
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-09 16:49:35.733:E/AndroidRuntime(1616):在
android.app.ActivityThread.access$600(ActivityThread.java:123)10-09
16:49:35.733:E/AndroidRuntime(1616):在
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-09 16:49:35.733:E/AndroidRuntime(1616):在
android.os.Handler.dispatchMessage(Handler.java:99)10-09
16:49:35.733:E/AndroidRuntime(1616):在
android.os.Looper.loop(Looper.java:137)10-09 16:49:35.733:
E/AndroidRuntime(1616):在
android.app.ActivityThread.main(ActivityThread.java:4424)10-09
16:49:35.733:E/AndroidRuntime(1616):在
java.lang.reflect.Method.Invokenactive(本机方法)10-09
16:49:35.733:E/AndroidRuntime(1616):在
java.lang.reflect.Method.invoke(Method.java:511)10-09 16:49:35.733:
E/AndroidRuntime(1616):在
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-09 16:49:35.733:E/AndroidRuntime(1616):在
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)10-09
16:49:35.733:E/AndroidRuntime(1616):在
dalvik.system.Nativestar.main(本地方法)10-09 16:49:35.733:
E/AndroidRuntime(1616):由以下原因引起:java.lang.NullPointerException
10-09 16:49:35.733:E/AndroidRuntime(1616):在
com.example.gps.gps.onCreate(gps.java:44)10-09 16:49:35.733:
E/AndroidRuntime(1616):在
android.app.Activity.performCreate(Activity.java:4465)10-09
16:49:35.733:E/AndroidRuntime(1616):在
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-09 16:49:35.733:E/AndroidRuntime(1616):在
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-09 16:49:35.733:E/AndroidRuntime(1616):。。。还有11个
看起来问题出在显示位置设置屏幕时 使用以下代码启用/禁用GPS 公开无效turnGPSOn() { } 公共空间的转换 {
}我想你可能已经找到了解决办法。但我为将来可能有相同问题的人发布了此答案。您只声明了
locationManager
,但尚未初始化它。但是您后来声明了新的LocationManager
调用了service
。因此,您可以将服务作为LocationManager
用于您的活动,也可以像这样初始化LocationManager
public class GPS extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
latituteField = (TextView) findViewById(R.id.TextView02);
longitudeField = (TextView) findViewById(R.id.TextView04);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);//now you have initialised locationManager, and is available for use.
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);//Declared and initialised new LocationManager service but only accessible inside onCreate method.
boolean enabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
Intent intent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria,false);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
Log.d("PROVIDER", "Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
} `
现在将不会出现任何空指针异常。您可以在此处附加崩溃日志吗?检查清单文件中的所有权限,并请附加日志。您已调用MainActivity类,但您想调用gps类。在AndroidMenifestFile中用GPS替换MainActivity我编辑帖子很抱歉格式不好,但我不知道如何以良好的格式发布。是的,我知道这一行会导致应用程序崩溃,但我不知道为什么。对不起,但问题不在这一部分。这一行的问题provider=locationManager.getBestProvider(criteria,false);它抛出空指针异常locationManager为空。您尚未初始化它。您可以使用下面的代码locationManager=(locationManager)getSystemService(Context.LOCATION\u SERVICE)初始化它;
String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps")){ //if gps is enabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
this.ctx.sendBroadcast(poke);
}
public class GPS extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
latituteField = (TextView) findViewById(R.id.TextView02);
longitudeField = (TextView) findViewById(R.id.TextView04);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);//now you have initialised locationManager, and is available for use.
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);//Declared and initialised new LocationManager service but only accessible inside onCreate method.
boolean enabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
Intent intent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria,false);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
Log.d("PROVIDER", "Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
} `