GPS崩溃活动

时间:2013-12-10 16:58:17

标签: android gps location

我正在尝试从android中的gps检索我的lat和lng的另一个全新尝试。使用新代码,它首先成功打开了我的活动,但我没有获得任何gps坐标。然后,我拔掉了手机的电话,将它移向窗户,最终崩溃了。然后我决定再试一次,但实际插入它,以便在窗口崩溃时获取错误报告。

我第二次启动应用程序时,我现在无法再进入活动而不会因为此错误而崩溃:

12-10 11:53:52.923      398-398/com.beerportfolio.beerportfoliopro E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.beerportfolio.beerportfoliopro/com.example.beerportfoliopro.FindBrewery}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2517)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1413)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5789)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.beerportfoliopro.FindBrewery.onLocationChanged(FindBrewery.java:67)
            at com.example.beerportfoliopro.FindBrewery.onCreate(FindBrewery.java:42)
            at android.app.Activity.performCreate(Activity.java:5195)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2473)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1413)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5789)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
            at dalvik.system.NativeStart.main(Native Method)
12-10 11:53:52.933  24060-24091/system_process E/EmbeddedLogger﹕ App crashed! Process: com.beerportfolio.beerportfoliopro
12-10 11:53:52.933  24060-24091/system_process E/EmbeddedLogger﹕ App crashed! Package: com.beerportfolio.beerportfoliopro v22 (3.0)
12-10 11:53:52.933  24060-24091/system_process E/EmbeddedLogger﹕ Application Label: BeerPortfolio Pro

我的活动代码是:

public class FindBrewery extends Activity implements LocationListener {
    private TextView latituteField;
    private TextView longitudeField;
    private LocationManager locationManager;
    private String provider;


    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.beer_location_list);
        //latituteField = (TextView) findViewById(R.id.TextView02);
        //longitudeField = (TextView) findViewById(R.id.TextView04);

        // Get the location manager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the locatioin provider -> use
        // default
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

        // Initialize the location fields
        if (location != null) {
            System.out.println("Provider " + provider + " has been selected.");
            onLocationChanged(location);
        } else {
            Toast.makeText(this, "Location Not available " + provider,
                    Toast.LENGTH_SHORT).show();
        }
    }

    /* Request updates at startup */
    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
    }

    /* Remove the locationlistener updates when Activity is paused */
    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude());
        int lng = (int) (location.getLongitude());
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));

        Toast.makeText(this, "Attempting async task" + provider,
                Toast.LENGTH_SHORT).show();

        //call asycn task for location
        String url = "myURL";

        Log.d("urlTest", url);

        //async task goes here
        new GetNearbyBreweries(this).execute(url);



    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(this, "Disabled provider " + provider,
                Toast.LENGTH_SHORT).show();
    }
} 

我想要完成的是,当活动开始时它会检索用户位置,然后将lng和lat发送到我的异步任务,然后从我的数据库中检索位置列表并填充列表。

2 个答案:

答案 0 :(得分:3)

你的问题在这里:

     Location location = locationManager.getLastKnownLocation(provider);

    // Initialize the location fields
    if (location != null) {
        System.out.println("Provider " + provider + " has been selected.");
        onLocationChanged(location);

您以编程方式呼叫onLocationChanged。这是错的。你真正想要的是locationManager.requestLocationUpdates。见这里:

http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String,%20long,%20float,%20android.location.LocationListener)

您的位置为空,因为您告诉您的应用您有位置,而实际上您没有。这样做会导致NullPointerException:

    int lat = (int) (location.getLatitude());
    int lng = (int) (location.getLongitude());
    latituteField.setText(String.valueOf(lat));
    longitudeField.setText(String.valueOf(lng));

如果您等待触发位置更新,那么它将正常工作。

答案 1 :(得分:0)

或者你可能没有添加获取gps位置所需的权限,它可能会给你NullPointerException,因为它是空的。