0

我开发了检测 android 设备位置并在文本视图中显示其值的简单应用程序

应用程序一旦开始抛出 RuntimeException 就会崩溃。

以下是 GPS.java

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): 致命异常: main 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.invokeNative(Native Method) 10-09 16:49 :35.733: E/AndroidRuntime(1616): at java.lang.reflect.Method.invoke(Method.java:511) 10-09 16:49:35.733: E/AndroidRuntime(1616): at com.android.internal。 os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 10-09 16:49:35.733: E/AndroidRuntime(1616): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 10 -09 16:49:35.733: E/AndroidRuntime(1616): at dalvik.system.NativeStart.main(Native Method) 10-09 16:49:35.733: E/AndroidRuntime(1616): Caused by: java.lang.NullPointerException 10-09 16:49:35.733: E/AndroidRuntime(1616): at 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 个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 更多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 更多

4

2 回答 2

0

显示位置设置屏幕时似乎有问题。

使用以下代码启用/禁用 GPS

公共无效turnGPSOn(){

 Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
 intent.putExtra("enabled", true);
 this.ctx.sendBroadcast(intent);

String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps"))
    { 
    //if gps is disabled
    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);
}

}

公共无效turnGPSOff(){

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);
}

}

于 2013-10-09T08:01:29.713 回答
0

我想你可能已经找到了你的解决方案。但是我为将来可能遇到相同类型问题的人发布了此答案。您仅声明locationManager但尚未初始化它。但是您后来声明了 newLocationManager称为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");
    } 
} `

现在不会有任何空指针异常。

于 2014-10-15T05:31:03.863 回答