我的Android应用程序崩溃了

时间:2015-02-18 11:44:34

标签: java android eclipse timer handler

我已经构建了一个运行服务的简单应用程序。在应用程序中,用户输入地址,服务需要计算用户位置与他输入的地址之间的距离。如果它们之间的距离小于100米,则会弹出一个Toast消息。 当我第一次运行应用程序时,它正确地提供了距离,但经过几次我在不同的地方运行我的应用程序后,距离的计算正在增长。 例如:我转向eclipse(在我家)然后在我的设备上运行应用程序。我输入我家的地址,在计算距离后,距离为5米,以便弹出Toast消息。然后我去某个地方并输入我去的地方的地址,当我到达那里时,吐司的混乱就会弹出。 但是当我回到我的地方并再次运行应用程序(带有我家的地址)时手机连接到eclipse,距离现在是20米而不是5米。 可能是什么问题?应用程序粉碎了一些时间....

MainActivity:

public void startMethod(View v)
{
    Bundle args = new Bundle();
    args.putParcelable("from_position", latLngC);
    args.putParcelable("to_position", latLngD);


    Intent i = new Intent(this, MainService.class);
    i.putExtra("bundle", args);
    startService(i);
}

MainService:

Toast.makeText(this, "service started", Toast.LENGTH_LONG).show(); // 
    Bundle bundle = intent.getParcelableExtra("bundle");

    latLngC = bundle.getParcelable("from_position");//Getting the latlng of user position
    latLngD = bundle.getParcelable("to_position");//Getting the latlng of destination position

    timer = new Timer();
    //schedule the timer, after the first 5000ms the TimerTask will run every 10000ms
    timer.scheduleAtFixedRate(
            timerTask = new TimerTask() {

                public void run() {
                    //use a handler to run a toast that shows the current timestamp

                    handler.post(new Runnable() {

                        public void run() {

                            latLngC=setMyLocation(); // Getting users current location
                            double r = CalculationByDistance(latLngC, latLngD);
                             if(r<0.1){
                                  Calendar calendar = Calendar.getInstance();
                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a");
                                final String strDate = simpleDateFormat.format(calendar.getTime());

                                //show the toast
                                int duration = Toast.LENGTH_SHORT;  
                                Toast toast = Toast.makeText(getApplicationContext(), strDate, duration);
                                toast.show();
                                        timer.cancel(); // stopping the timer 
                                        stopSelf();  // stopping the service
                            }

                        }
                    });
                }
            }
            , 10000, 10000); //

    return super.onStartCommand(intent, flag, startId);
}

同样在MainService中:

public double CalculationByDistance(LatLng StartP, LatLng EndP) {
    int Radius=6371;//radius of earth in Km         
    double lat1 = StartP.latitude;
    double lat2 = EndP.latitude;
    double lon1 = StartP.longitude;
    double lon2 = EndP.longitude;
    double dLat = Math.toRadians(lat2-lat1);
    double dLon = Math.toRadians(lon2-lon1);
    double a = 0;
    a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
    Math.sin(dLon/2) * Math.sin(dLon/2);
    double c = 0;
    c = 2 * Math.asin(Math.sqrt(a));
    double valueResult= Radius*c;
    double km=valueResult/1;
    DecimalFormat newFormat = new DecimalFormat("####");
    int kmInDec =  Integer.valueOf(newFormat.format(km));
    double meter=valueResult%1000;
    int  meterInDec= Integer.valueOf(newFormat.format(meter));
    Log.i("Radius Value",""+valueResult+"   KM  "+kmInDec+" Meter   "+meterInDec);

    return Radius * c;
 }

logcat的:

 02-18 13:49:29.538: E/AndroidRuntime(13037): FATAL EXCEPTION: main
02-18 13:49:29.538: E/AndroidRuntime(13037): java.lang.NullPointerException
02-18 13:49:29.538: E/AndroidRuntime(13037):    at com.*******drive.***.MainActivity$GeocoderTask.onPostExecute(MainActivity.java:231)
02-18 13:49:29.538: E/AndroidRuntime(13037):    at com.*******drive.***.MainActivity$GeocoderTask.onPostExecute(MainActivity.java:1)
02-18 13:49:29.538: E/AndroidRuntime(13037):    at android.os.AsyncTask.finish(AsyncTask.java:631)
02-18 13:49:29.538: E/AndroidRuntime(13037):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-18 13:49:29.538: E/AndroidRuntime(13037):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-18 13:49:29.538: E/AndroidRuntime(13037):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 13:49:29.538: E/AndroidRuntime(13037):    at android.os.Looper.loop(Looper.java:137)
02-18 13:49:29.538: E/AndroidRuntime(13037):    at android.app.ActivityThread.main(ActivityThread.java:4867)
02-18 13:49:29.538: E/AndroidRuntime(13037):    at java.lang.reflect.Method.invokeNative(Native Method)
02-18 13:49:29.538: E/AndroidRuntime(13037):    at java.lang.reflect.Method.invoke(Method.java:511)
02-18 13:49:29.538: E/AndroidRuntime(13037):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
02-18 13:49:29.538: E/AndroidRuntime(13037):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
02-18 13:49:29.538: E/AndroidRuntime(13037):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

问题可能在于您使用setMyLocation()获取当前位置(在方法getLastKnownLocation中)。然后,您将获得最后的位置,这可能不是您当前的位置。如果这是问题,首先,您需要获取实际位置(有很多方法,例如使用onLocationChanged),然后计算距离并显示Toasts ......

此外,当应用程序崩溃时发布您的logcat ...

相关问题