繁体   English   中英

我的Android应用程式当机

[英]My android app crashes

我已经构建了一个运行服务的简单应用程序。 在应用程序中,用户输入一个地址,服务需要计算用户位置和他输入的地址之间的距离。 如果它们之间的距离小于100米,则会弹出敬酒消息。 当我第一次运行该应用程序时,它可以正确提供距离,但是经过几次在不同地方运行我的应用程序后,距离的计算却在增长。 例如:我在家里打开eclipse,然后在设备上运行该应用程序。 我输入自己的住所地址,计算出距离后,距离为5米,所以会弹出吐司消息。 然后我去某个地方,输入我要去的地方的地址,当我到达那儿时,吐司便会弹出。 但是,当我回到自己的位置并在连接了Eclipse的手机上再次运行该应用程序(使用我家的地址)时,距离现在是20米而不是5米。 可能是什么问题? 然后应用崩溃了。

主要活动:

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)

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

另外,在应用程序崩溃时发布您的logcat ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM