碰到我怀疑onLocationChanged不知道bug是什么

时间:2016-05-03 13:57:16

标签: android gps

帮助!这是我在简单的Udacity课程之外的第一个应用程序,请善待。

我准确地想要gps一次。我的应用程序的用户不需要gps刷新。不过我相信我需要一个onLocationChanged监听器来获得准确的gps读取。

它崩溃了。在MainActivity中,它可以在调用MyLocationListener类之后命中第二个Toast。这表明该错误发生在MainActivity的getGPS中。但我没有看到哪里。我认为听众允许MainActivity在搜索GPS时继续?

手机上的gps正在运行我已经下载了测试版应用。我看起来很漂亮,把房子留在我的Y战线上。

这是来自crashalytics的堆栈跟踪。

http://crashes.to/s/54d65de993b

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
   at android.widget.Toast.<init>(Toast.java:101)
   at android.widget.Toast.makeText(Toast.java:258)
   at com.example.android.carfinder.MyLocationListener.onLocationChanged(MyLocationListener.java:28)
   at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:281)
   at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:210)
   at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:226)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:135)
   at android.app.ActivityThread.main(ActivityThread.java:5254)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

所以问题,

    1. what's causing the crash?
    2. When I've got a gps, how do I turn the listener off, till the button is re-pressed? 
    3. Why doesn't progressDialog show while it searches for gps?

MainActivity,getGPS使用onClick事件激活:

 public void getGPS(View view) {
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    getSystemService(Context.LOCATION_SERVICE);

    if(displayGpsStatus() == false) {
        Log.i("getGPS", "GPS turned off");
        return;
    }
    // GPS is working
    //progressBar.setVisibility(View.VISIBLE);
    ProgressDialog progressDialog = ProgressDialog.show(this, "Getting GPS lock", "Not much longer love");

    Log.i("getGPS", "trying for new co-ords");
    LocationListener locationListener = new MyLocationListener();
    try {
        Toast.makeText(this, "activating locationListener", Toast.LENGTH_SHORT).show();
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 0, 0, locationListener); //5000, 10
        Log.i("getGPS", "just returned from MyLocationListener Class");
    }
    catch (SecurityException e) {
        Log.i("getGPS", "no permission to use GPS");
        progressBar.setVisibility(View.GONE);
        Toast.makeText(this, "Did you block GPS permission on install?", Toast.LENGTH_LONG).show();
        progressDialog.dismiss();
        return;
    }
    Toast.makeText(this, "continuing, is address and time updated?", Toast.LENGTH_LONG).show();
    // Got GPS so hide the progress bar
    //progressBar.setVisibility(View.GONE);
    progressDialog.dismiss();

    // update the date and address views
    //saveDateTime();
    //updateAddressView();
    return;
}

MyLocationListener:

public class MyLocationListener extends MainActivity implements LocationListener {
     String TAG = "MyLocationListener Class";

@Override
public void onLocationChanged(Location loc) {
    Log.i("onLocationChanged", "in outside class");

    Toast.makeText(
            getBaseContext(),
            "Location changed: Lat: " + loc.getLatitude() + " Lng: "
                    + loc.getLongitude(), Toast.LENGTH_SHORT).show();
    String longitude = "Longitude: " + loc.getLongitude();
    Log.i(TAG, longitude);
    String latitude = "Latitude: " + loc.getLatitude();
    Log.i(TAG, latitude);
    // TODO need to check latitude and longitude accessible from MainActivity

    /*------- To get city name from coordinates -------- */
    // from here we're reverse geo-coding to get the address
    String address = null;
    String city = null;
    Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
    List<Address> addresses;
    try {
        addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
        if (addresses.size() > 0) {
            //System.out.println(addresses.get(0).getLocality());
            address = addresses.get(0).getAddressLine(0);
            city = addresses.get(0).getLocality();
        }
    }
    catch (IOException e) {
        e.printStackTrace();
        Log.i("MyLocationListener", "No address found");
    }
    String savedAddress = address + ",  " + city;
    if(address == null && city == null) {
        savedAddress = "couldn't fathom address, tap view on map to check";
    }
    TextView addressTextView = (TextView) findViewById(R.id.approxAddress);
    addressTextView.setText(savedAddress);
    // save it to permanent memory
    SharedPreferences.Editor mEditor = mPrefs.edit();
    mEditor.putString("savedAddress", savedAddress).apply();
    mEditor.putString("savedLongitude", longitude).apply();
    mEditor.putString("savedLatitude", latitude).apply();

    Log.i("MyLocationListener", "SAVED address, gps: ");
}

@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(getBaseContext(), "MyLocListener: GPS disabled DOH!", Toast.LENGTH_LONG).show();
}

@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(getBaseContext(), "MyLocListener: GPS enabled :)", Toast.LENGTH_LONG).show();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    Toast.makeText(getBaseContext(), "MyLocListener: GPS status changed?", Toast.LENGTH_LONG).show();
}
}

1 个答案:

答案 0 :(得分:1)

错误来自对Toast.makeText的调用。我的猜测是,您在创建活动之前尝试使用Toast(并使用它getBaseContext)。那时的背景还不存在。

尝试通过Toast.makeText来电替换Log.i来电。如果它消除了错误,并且您确实需要Toast,则只需在调用Toast.makeText之前检查上下文是否为空。