android应用程序在终止时停止

时间:2015-11-15 20:19:58

标签: android

我的应用程序有点问题。当我在手机中使用它时,它可以完全正常工作,但当我按下一个带有" finish()"命令它停止。当我试图重新打开它时,它表示它有一个零点异常。而且我不知道为什么。这是带有例外的代码。

public class MapAlertService extends Service {

    public static final String HOME_LONGITUDE = "HOME_LONGITUDE";
    public static final String HOME_LATITUDE = "HOME_LATITUDE";
    public static final String USER_NUMBER = "USER_NUMBER";
    private final String PROX_ALERT = "com.emmanuilvaresis.oldiefinder.ProximityAlert";
    SharedPreferences prefs;
    Context context;
    double lat = 0;
    double lng = 0;
    double hlat = 0;
    double hlng = 0;
    float radius = 80f; // meters
    long expiration = -1; // do not expire
    private LocationManager locationManager;
    PackageManager packageManager;

    static int flag = 0;

    @Override
    public void onCreate(){
        context = getApplicationContext();
        Toast.makeText(context, "map alert service Created", Toast.LENGTH_LONG).show();
        prefs = PreferenceManager.getDefaultSharedPreferences(context);
        LocationManager locationManager;
        String svcName = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) getSystemService(svcName);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setSpeedRequired(false);
        criteria.setCostAllowed(true);
        String provider = locationManager.getBestProvider(criteria, true);
        PackageManager packageManager = getPackageManager();
        if (packageManager.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION, getPackageName())
                == PackageManager.PERMISSION_GRANTED){
            Location l = locationManager.getLastKnownLocation(provider);
            getCoordinates(l);
            locationManager.requestLocationUpdates(provider, 10, 10, locationListener);
        }
    }

    private void getCoordinates(Location l){
        if (l != null) {
            lat = l.getLatitude();
            lng = l.getLongitude();
        }
        double templat, templng;
        templat = prefs.getFloat(HOME_LATITUDE, 0);
        templng = prefs.getFloat(HOME_LONGITUDE, 0);
        if((hlat != templat) && (hlng != templng)){
            hlat = templat;
            hlng = templng;
            if((hlat!=0) && (hlng!=0)){
                addProximityAlert(hlat, hlng);
            }
        }
    }

    private final LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            getCoordinates(location);
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {
        }
    };

    private void addProximityAlert(double latitude, double longitude) {
        if (packageManager.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION, getPackageName())
                == PackageManager.PERMISSION_GRANTED){
            Intent intent = new Intent(PROX_ALERT);
            PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
            locationManager.addProximityAlert(latitude, longitude, radius, expiration, proximityIntent);
            IntentFilter filter = new IntentFilter(PROX_ALERT);
            registerReceiver(new MapAlertReceiver(), filter);
        }
    }

    public class MapAlertReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            final String key = LocationManager.KEY_PROXIMITY_ENTERING;
            final Boolean entering = intent.getBooleanExtra(key, false);
            if (!entering){
                Toast.makeText(context,"You are exiting the safe circle", Toast.LENGTH_SHORT).show();
                if (flag == 0){
                    String msgBody = "The user is 1km far from his house";
                    String number = prefs.getString(USER_NUMBER, "0");
                    try {
                        if ((lat!=0) && (lng!=0) && !(number.equals("0"))){
                            SmsManager smsManager = SmsManager.getDefault();
                            smsManager.sendTextMessage(number, null, msgBody, null, null);
                            Toast.makeText(context, "Message sent", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            Toast.makeText(context, "Message didn't sent", Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    flag = 1;
                }
            }
            else{
                Toast.makeText(context,"You are entering the safe circle", Toast.LENGTH_SHORT).show();
                flag = 0;
            }
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

logcat文件:

11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime: FATAL EXCEPTION: main
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime: Process: com.emmanuilvaresis.oldiefinder, PID: 28905
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime: java.lang.RuntimeException: Unable to create service com.emmanuilvaresis.oldiefinder.MapAlertService: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.pm.PackageManager.checkPermission(java.lang.String, java.lang.String)' on a null object reference
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at android.app.ActivityThread.handleCreateService(ActivityThread.java:3160)
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at android.app.ActivityThread.access$1900(ActivityThread.java:177)
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1531)
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5944)
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.pm.PackageManager.checkPermission(java.lang.String, java.lang.String)' on a null object reference
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at com.emmanuilvaresis.oldiefinder.MapAlertService.addProximityAlert(MapAlertService.java:102)
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at com.emmanuilvaresis.oldiefinder.MapAlertService.getCoordinates(MapAlertService.java:77)
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at com.emmanuilvaresis.oldiefinder.MapAlertService.onCreate(MapAlertService.java:60)
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at android.app.ActivityThread.handleCreateService(ActivityThread.java:3150)
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at android.app.ActivityThread.access$1900(ActivityThread.java:177) 
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1531) 
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145) 
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5944) 
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389) 
11-15 22:10:31.779 28905-28905/com.emmanuilvaresis.oldiefinder E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184) 

1 个答案:

答案 0 :(得分:3)

onCreate中的局部变量packageManager中的

隐藏了声明为成员类的那个,使其初始化,并导致NPE addProximityAlert。内部onCreate更改

PackageManager packageManager = getPackageManager();

packageManager = getPackageManager();