如何解决应用程序获得坠毁 [英] How to solve app get crashed

查看:92
本文介绍了如何解决应用程序获得坠毁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在开发的GPS跟踪应用程序。当我preSS的登录按钮,它具有显示经纬度和准确性。它不能正常工作,应用越来越crashed.Please帮我解决这个问题。先谢谢了。

这是我的activity.class:

  btnShowLocation =(按钮)findViewById(R.id.btnShowLocation);
        //显示地点按钮点击事件
        btnShowLocation.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){


                AndroidGPSTrackingActivity.this.startService(新意图(AndroidGPSTrackingActivity.this,GPSTracker.class));
                //创建类对象
                 // MyTimerTask MyTask的=新MyTimerTask();
                 上下文C = getApplicationContext();
                  共享preferences APP_ preferences = preferenceManager.getDefaultShared preferences(C);
                @燮pressWarnings(未使用)
                共享preferences.Editor编辑= APP_ preferences.edit();
                    TimerTask的scanTask;
                    最终的处理程序处理程序=新的处理程序();
                    计时器t =新的Timer();
                    scanTask =新的TimerTask(){
                        GPSTracker GPS;
                        名单<双> L =新的ArrayList<双>();
                        TreeMap的<双,名单,其中,双>>名单=新TreeMap的<双,名单,其中,双>>();
                             公共无效的run(){
                                    handler.post(新的Runnable(){
                                           // @燮pressLint(UseValueOf)
                                            @燮pressLint(UseValueOf)
                                            公共无效的run(){
                                                  上下文C = getApplicationContext();
                                                  共享preferences APP_ preferences = preferenceManager.getDefaultShared preferences(C);
                                                共享preferences.Editor编辑= APP_ preferences.edit();
                                                INT计数器= APP_ preferences.getInt(反,0);
                                              Log.i(日志,maaaap+清单);
                                                双纬度=新的双(0.0D);
                                                双经度=新的双(0.0D);
                                                    L =新的ArrayList<双>();
                                                    GPS =新GPSTracker(AndroidGPSTrackingActivity.this);
                                                    //检查是否启用GPS
                                                    如果(gps.canGetLocation()){

                                                         l.add(gps.getLatitude());
                                                         l.add(gps.getLongitude());
                                                         list.put(gps.getAccur(),升);
                                                         Toast.makeText(getApplicationContext(),您现在的位置是 -  \新工人艺术团:+ gps.getLatitude()+\ nLong:+ gps.getLongitude()+\ nAccracy:+ gps.getAccur(),烤面包。 LENGTH_SHORT).show();

                                                         Log.i(日志,纬度+ gps.getLatitude());
                                                         Log.i(日志,经度+ gps.getLongitude());
                                                         Log.i(日志,gps.getAccur()+ gps.getAccur());
                                                         Log.d(标签,寻找纬度);
                                                         纬度= gps.getLatitude();
                                                        Log.d(标签,纬度:+将String.valueOf(纬度));
                                                        Log.d(标签,寻找经度);
                                                        经度= gps.getLongitude();
                                                        Log.d(标签,经度:+将String.valueOf(经度));
                                                        文本字符串=
                                                        \新工人艺术团:+ gps.getLatitude()+
                                                        \ nLan:+ gps.getLongitude()+\ nAcrcy =+ gps.getAccur();
                                                       //txtData.setText(Text);


                                                        sendSMS(readfromFilesms(),文本);

                                                    }

                                                如果(柜< 12)
                                                  {
                                                    如果(gps.getAccur()&其中; = 3.0D)
                                                    {
                                                        双D =(双人间)list.firstKey();
                                                        Log.i(日志,gps.firstKey()+ D);
                                                        @燮pressWarnings(rawtypes)
                                                        列表L1 =(列表)list.get(四);
                                                        Log.i(日志,gps.l1()+(双人间)l1.get(0));
                                                        Log.i(日志,gps.l2+(双人间)l1.get(1));
                                                        纬度=(双人间)l1.get(0);
                                                        经度=(双人间)l1.get(1);
                                                       //的String [] S = {manjugowda.manju2@gmail.com};
                                                        //SendMail(s,latitude.toString(),longitude.toString(),"22);
                                                        editor.putInt(反,0);
                                                        editor.commit(); // 很重要
                                                        完();
                                                        //Toast.makeText(getApplicationContext(),您现在的位置是 -  \ nLong:+ D,Toast.LENGTH_LONG).show();
                                                    }
                                                    其他
                                                    {
                                                        计数器= APP_ preferences.getInt(反,0);
                                                        Log.i(日志,反+计数器);
                                                        editor.putInt(专柜,专柜++);
                                                        editor.commit();
                                                        完成(); //非常重要
                                                    }

                                                  }
                                                其他
                                                {
                                                    双D =(双人间)list.firstKey();
                                                    Log.i(日志,gps.firstKey()+ D);
                                                    @燮pressWarnings(rawtypes)
                                                    列表L1 =(列表)list.get(四);
                                                    Log.i(日志,gps.l1()+(双人间)l1.get(0));
                                                    Log.i(日志,gps.l2+(双人间)l1.get(1));
                                                    纬度=(双人间)l1.get(0);
                                                    经度=(双人间)l1.get(1);
                                                    // @燮pressWarnings(未使用)
                                                    //的String [] S = {manjugowda.manju2@gmail.com};
                                                   //的SendMail(S,latitude.toString(),longitude.toString(),22);
                                                    editor.putInt(反,0);
                                                    editor.commit(); // 很重要
                                                    完();
                                                    //Toast.makeText(getApplicationContext(),你Accurecy是 -  \ n为+ D,Toast.LENGTH_LONG).show();
                                                }
                                            }
                                   });
                            }};
                        t.schedule(scanTask,1000,90000000);
                        完();
        }
        });
 

logcat的错误:

  04-08 18:27:01.438:E / ActivityThread(23915):在java.lang.reflect.Method.invoke(Method.java:515)
04-08 18:27:01.438:E / ActivityThread(23915):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)
04-08 18:27:01.438:E / ActivityThread(23915):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-08 18:27:01.438:E / ActivityThread(23915):在dalvik.system.NativeStart.main(本机方法)
04-08 18:27:01.904:I /日志(23915):maaaap {}
04-08 18:27:01.925:D / GPS功能的(23915):GPS已启用
04-08 18:27:01.925:我/的System.out(23915):里面的位置管理器
04-08 18:27:01.929:D / AndroidRuntime(23915):关闭虚拟机
04-08 18:27:01.929:W / dalvikvm(23915):主题ID = 1:螺纹退出与未捕获的异常(组= 0x416c8d40)
04-08 18:27:01.948:E / AndroidRuntime(23915):致命异常:主要
04-08 18:27:01.948:E / AndroidRuntime(23915):工艺:com.example.gpstracking,PID:23915
04-08 18:27:01.948:E / AndroidRuntime(23915):显示java.lang.NullPointerException
04-08 18:27:01.948:E / AndroidRuntime(23915):在com.example.gpstracking.AndroidGPSTrackingActivity $ 1 $ 1 $ 1.运行(AndroidGPSTrackingActivity.java:163)
04-08 18:27:01.948:E / AndroidRuntime(23915):在android.os.Handler.handleCallback(Handler.java:733)
04-08 18:27:01.948:E / AndroidRuntime(23915):在android.os.Handler.dispatchMessage(Handler.java:95)
04-08 18:27:01.948:E / AndroidRuntime(23915):在android.os.Looper.loop(Looper.java:136)
04-08 18:27:01.948:E / AndroidRuntime(23915):在android.app.ActivityThread.main(ActivityThread.java:5102)
04-08 18:27:01.948:E / AndroidRuntime(23915):在java.lang.reflect.Method.invokeNative(本机方法)
04-08 18:27:01.948:E / AndroidRuntime(23915):在java.lang.reflect.Method.invoke(Method.java:515)
04-08 18:27:01.948:E / AndroidRuntime(23915):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)
04-08 18:27:01.948:E / AndroidRuntime(23915):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-08 18:27:01.948:E / AndroidRuntime(23915):在dalvik.system.NativeStart.main(本机方法)
04-08 18:27:03.605:I /流程(23915):发送信号。 PID:23915 SIG:9
 

GPSTracker.class:

 公共类GPSTracker扩展服务实现LocationListener的{

    私人最终语境mContext;

    //标志GPS状态
    布尔isGPSEnabled = FALSE;

    //标志网络状态
    布尔isNetworkEnabled = FALSE;

    //标志GPS状态
    布尔canGetLocation = FALSE;

    地点位置; // 位置
    双纬度; // 纬度
    双经度; // 经度
    双ACCUR;

    公共双getAccur(){
        返回ACCUR;
    }

    公共无效setAccur(双人间ACCUR){
        this.accur = ACCUR;
    }

    //最小距离改变以米为单位更新
    私有静态最后长MIN_DISTANCE_CHANGE_FOR_UPDATES = 12; //10米

    //毫秒更新之间的最短时间
    私有静态最后长MIN_TIME_BW_UPDATES = 0; // 1秒

    //声明一个位置管理
    保护LocationManager locationManager;

    公共对象getAccuracy;

    公共GPSTracker(上下文的背景下){
        this.mContext =背景;
        的getLocation();
    }

    公共场所的getLocation(){
        尝试 {
            locationManager =(LocationManager)mContext
                    .getSystemService(LOCATION_SERVICE);

            //获取GPS状态
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            //获取网络状态
            / * isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            / *如果(isGPSEnabled&安培;!&安培;!isNetworkEnabled){
                //没有网络提供商启用
            } 其他 {
                /*this.canGetLocation = TRUE;
                如果(isNetworkEnabled){
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);
                    Log.d(网络,网络);
                    如果(locationManager!= NULL){
                        位置= locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        如果(位置!= NULL){
                            的System.out.println(内的位置);
                            纬度= location.getLatitude();

                            的System.out.println(纬度+纬度);
                            经度= location.getLongitude();
                        }
                    }
                } * /
                如果(isGPSEnabled){
                    如果(位置== NULL){

                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);
                        Log.d(GPS功能的,GPS功能的);

                        如果(locationManager!= NULL){

                            的System.out.println(里的位置经理);
                            位置= locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            如果(位置!= NULL){

                                的System.out.println(内的位置);
                                纬度= location.getLatitude();

                                的System.out.println(纬度+纬度);
                                经度= location.getLongitude();
                            }
                        }
                    }
                }

            如果(位置!= NULL)
            {
                setAccur(新双(location.getAccuracy()));
            }

        }赶上(例外五){
            e.printStackTrace();
        }

        返回的位置;
    }

    / **
     *停止使用GPS监听器
     *调用此函数将停止使用GPS在你的应用程序
     * * /
    / *公共无效stopUsingGPS(){
        如果(locationManager!= NULL){
            locationManager.removeUpdates(GPSTracker.this);
        }
    } * /

    / **
     *功能得到纬度
     * * /
    公共双getLatitude(){
        如果(位置!= NULL){
            纬度= location.getLatitude();
        }

        //返回纬度
        返回纬度;
    }

    / **
     *功能得到经度
     * * /
    公共双getLongitude(){
        如果(位置!= NULL){
            经度= location.getLongitude();
        }

        //返回经度
        返回经度;
    }

    / **
     *功能检查GPS / WiFi功能
     * @返回布尔
     * * /
    公共布尔canGetLocation(){
        返回this.canGetLocation;
    }

    / **
     *功能,以显示设置警告对话框
     *在pressing设置按钮将劳克设置选项
     * * /
    公共无效showSettingsAlert(){
        AlertDialog.Builder alertDialog =新AlertDialog.Builder(mContext);

        //设置对话框标题
        alertDialog.setTitle(全球定位系统设置);

        //设置对话框消息
        alertDialog.setMessage(GPS未启用你想进入设置菜单。?);

        //在pressing设置按钮
        alertDialog.setPositiveButton(设置,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释它){
                意向意图=新的意图(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(意向);
            }
        });

        //在pressing取消按钮
       / * alertDialog.setNegativeButton(取消,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释它){
            dialog.cancel();
            }
        }); * /

        //显示警报信息
        alertDialog.show();
    }

    @覆盖
    公共无效onLocationChanged(位置定位){
    }

    @覆盖
    公共无效onProviderDisabled(字符串提供商){
    }

    @覆盖
    公共无效onProviderEnabled(字符串提供商){
    }

    @覆盖
    公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
    }

    @覆盖
    公众的IBinder onBind(意向为arg0){
        返回null;
    }

}
 

解决方案

根据您的code。我发现它的地方越来越应声问题。可能是您的GPS的实例为空或得到改变为null。检查这首

 如果(GPS!= NULL){
  //你的code
}其他{
   Log.i(你的班组长姓名::,GPS对象为空)
}
 

我相信你会得到日志信息是GPS对象是空的。

如果仍然得到空,则重新初始化GPS对象。

Am developing an GPS Tracking application. When i press the the login button it has to show latitude longitude and accuracy. its not working correctly, app getting crashed.Please help me to solve this problem. Thanks in advance.

here is my activity.class:

btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
        // show location button click event
        btnShowLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                AndroidGPSTrackingActivity.this.startService(new Intent(AndroidGPSTrackingActivity.this, GPSTracker.class));
                // create class object
                 // MyTimerTask myTask = new MyTimerTask();
                 Context c = getApplicationContext();
                  SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(c);
                @SuppressWarnings("unused")
                SharedPreferences.Editor editor = app_preferences.edit();
                    TimerTask scanTask;
                    final Handler handler = new Handler();
                    Timer t = new Timer();
                    scanTask = new TimerTask() {
                        GPSTracker gps;
                        List<Double> l = new ArrayList<Double>();
                        TreeMap<Double,List<Double>> list = new  TreeMap<Double,List<Double>>();
                             public void run() {
                                    handler.post(new Runnable() {
                                           // @SuppressLint("UseValueOf")
                                            @SuppressLint("UseValueOf")
                                            public void run() {
                                                  Context c = getApplicationContext();
                                                  SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(c);
                                                SharedPreferences.Editor editor = app_preferences.edit();
                                                int counter = app_preferences.getInt("counter", 0);
                                              Log.i("log", "maaaap"+list);
                                                Double latitude=new Double(0.0d);
                                                Double longitude=new Double(0.0d);
                                                    l = new ArrayList<Double>();
                                                    gps = new GPSTracker(AndroidGPSTrackingActivity.this);
                                                    // check if GPS enabled     
                                                    if(gps.canGetLocation()){

                                                         l.add(gps.getLatitude());
                                                         l.add(gps.getLongitude());
                                                         list.put(gps.getAccur(),l);
                                                         Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + gps.getLatitude() + "\nLong:" + gps.getLongitude()+ "\nAccracy:"+gps.getAccur(), Toast.LENGTH_SHORT).show();

                                                         Log.i("log", "latitude"+gps.getLatitude());
                                                         Log.i("log", "longitude"+gps.getLongitude());
                                                         Log.i("log", "gps.getAccur()"+gps.getAccur());
                                                         Log.d("tag", "Finding Latitude");
                                                         latitude = gps.getLatitude();
                                                        Log.d("tag", "Lat: "+String.valueOf(latitude));
                                                        Log.d("tag", "Finding Longitude");
                                                        longitude  = gps.getLongitude();
                                                        Log.d("tag", "Lon: "+String.valueOf(longitude));
                                                        String Text =
                                                        "\nLat: " + gps.getLatitude() +
                                                        "\nLan:" + gps.getLongitude()+"\nAcrcy="+gps.getAccur();
                                                       //txtData.setText(Text);


                                                        sendSMS(readfromFilesms(), Text);

                                                    }

                                                if(counter<12)
                                                  {
                                                    if(gps.getAccur()<=3.0D)
                                                    {
                                                        Double  d =  (Double)list.firstKey();
                                                        Log.i("log", "gps.firstKey()"+d);
                                                        @SuppressWarnings("rawtypes")
                                                        List l1 = (List)list.get(d);
                                                        Log.i("log", "gps.l1()"+(Double)l1.get(0));
                                                        Log.i("log", "gps.l2"+(Double)l1.get(1));
                                                        latitude = (Double)l1.get(0);
                                                        longitude = (Double)l1.get(1);
                                                       // String[] s = {"manjugowda.manju2@gmail.com"};
                                                        //SendMail(s,latitude.toString(),longitude.toString(),"22");
                                                        editor.putInt("counter", 0);
                                                        editor.commit(); // Very important
                                                        finish();
                                                        //Toast.makeText(getApplicationContext(), "Your Location is - \nLong: "+d , Toast.LENGTH_LONG).show();
                                                    }
                                                    else
                                                    {
                                                        counter = app_preferences.getInt("counter", 0);
                                                        Log.i("log", "counter"+counter);
                                                        editor.putInt("counter", ++counter);
                                                        editor.commit();
                                                        finish();// Very important
                                                    }

                                                  }
                                                else
                                                {
                                                    Double  d =  (Double)list.firstKey();
                                                    Log.i("log", "gps.firstKey()"+d);
                                                    @SuppressWarnings("rawtypes")
                                                    List l1 = (List)list.get(d);
                                                    Log.i("log", "gps.l1()"+(Double)l1.get(0));
                                                    Log.i("log", "gps.l2"+(Double)l1.get(1));
                                                    latitude = (Double)l1.get(0);
                                                    longitude = (Double)l1.get(1);
                                                    //@SuppressWarnings("unused")
                                                    //String[] s = {"manjugowda.manju2@gmail.com"};
                                                   // SendMail(s,latitude.toString(),longitude.toString(),"22");
                                                    editor.putInt("counter", 0);
                                                    editor.commit(); // Very important
                                                    finish();
                                                    //Toast.makeText(getApplicationContext(), "Your Accurecy is - \n: "+d , Toast.LENGTH_LONG).show();
                                                }
                                            }
                                   });
                            }};
                        t.schedule(scanTask,1000,90000000);  
                        finish();
        }
        });

logcat error:

04-08 18:27:01.438: E/ActivityThread(23915):    at java.lang.reflect.Method.invoke(Method.java:515)
04-08 18:27:01.438: E/ActivityThread(23915):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
04-08 18:27:01.438: E/ActivityThread(23915):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-08 18:27:01.438: E/ActivityThread(23915):    at dalvik.system.NativeStart.main(Native Method)
04-08 18:27:01.904: I/log(23915): maaaap{}
04-08 18:27:01.925: D/GPS Enabled(23915): GPS Enabled
04-08 18:27:01.925: I/System.out(23915): inside location manager
04-08 18:27:01.929: D/AndroidRuntime(23915): Shutting down VM
04-08 18:27:01.929: W/dalvikvm(23915): threadid=1: thread exiting with uncaught exception (group=0x416c8d40)
04-08 18:27:01.948: E/AndroidRuntime(23915): FATAL EXCEPTION: main
04-08 18:27:01.948: E/AndroidRuntime(23915): Process: com.example.gpstracking, PID: 23915
04-08 18:27:01.948: E/AndroidRuntime(23915): java.lang.NullPointerException
04-08 18:27:01.948: E/AndroidRuntime(23915):    at com.example.gpstracking.AndroidGPSTrackingActivity$1$1$1.run(AndroidGPSTrackingActivity.java:163)
04-08 18:27:01.948: E/AndroidRuntime(23915):    at android.os.Handler.handleCallback(Handler.java:733)
04-08 18:27:01.948: E/AndroidRuntime(23915):    at android.os.Handler.dispatchMessage(Handler.java:95)
04-08 18:27:01.948: E/AndroidRuntime(23915):    at android.os.Looper.loop(Looper.java:136)
04-08 18:27:01.948: E/AndroidRuntime(23915):    at android.app.ActivityThread.main(ActivityThread.java:5102)
04-08 18:27:01.948: E/AndroidRuntime(23915):    at java.lang.reflect.Method.invokeNative(Native Method)
04-08 18:27:01.948: E/AndroidRuntime(23915):    at java.lang.reflect.Method.invoke(Method.java:515)
04-08 18:27:01.948: E/AndroidRuntime(23915):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
04-08 18:27:01.948: E/AndroidRuntime(23915):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-08 18:27:01.948: E/AndroidRuntime(23915):    at dalvik.system.NativeStart.main(Native Method)
04-08 18:27:03.605: I/Process(23915): Sending signal. PID: 23915 SIG: 9

GPSTracker.class:

public class GPSTracker extends Service implements LocationListener {

    private final Context mContext;

    // flag for GPS status
    boolean isGPSEnabled = false;

    // flag for network status
    boolean isNetworkEnabled = false;

    // flag for GPS status
    boolean canGetLocation = false;

    Location location; // location
    double latitude; // latitude
    double longitude; // longitude
    Double accur;

    public Double getAccur() {
        return accur;
    }

    public void setAccur(Double accur) {
        this.accur = accur;
    }

    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 12; // 10 meters

    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 0; // 1 sec

    // Declaring a Location Manager
    protected LocationManager locationManager;

    public Object getAccuracy;

    public GPSTracker(Context context) {
        this.mContext = context;
        getLocation();
    }

    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            /*isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            /*if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                /*this.canGetLocation = true;
                if(isNetworkEnabled){
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network","Network");
                    if(locationManager!=null){
                        location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if(location!=null){
                            System.out.println("inside location");
                            latitude = location.getLatitude();

                            System.out.println("lat"+latitude);
                            longitude = location.getLongitude();
                        }
                    }
                }*/
                if (isGPSEnabled) {
                    if (location == null) {

                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled","GPS Enabled");

                        if (locationManager != null) {

                            System.out.println("inside location manager");
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {

                                System.out.println("inside location");
                                latitude = location.getLatitude();

                                System.out.println("lat"+latitude);
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }

            if(location!=null)
            {
                setAccur(new Double(location.getAccuracy()));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }

    /**
     * Stop using GPS listener
     * Calling this function will stop using GPS in your app
     * */
    /*public void stopUsingGPS(){
        if(locationManager != null){
            locationManager.removeUpdates(GPSTracker.this);
        }       
    }*/

    /**
     * Function to get latitude
     * */
    public double getLatitude(){
        if(location != null){
            latitude = location.getLatitude();
        }

        // return latitude
        return latitude;
    }

    /**
     * Function to get longitude
     * */
    public double getLongitude(){
        if(location != null){
            longitude = location.getLongitude();
        }

        // return longitude
        return longitude;
    }

    /**
     * Function to check GPS/wifi enabled
     * @return boolean
     * */
    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    /**
     * Function to show settings alert dialog
     * On pressing Settings button will lauch Settings Options
     * */
    public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

        // Setting Dialog Title
        alertDialog.setTitle("GPS is settings");

        // Setting Dialog Message
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

        // On pressing Settings button
        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(intent);
            }
        });

        // on pressing cancel button
       /* alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
            }
        });*/

        // Showing Alert Message
        alertDialog.show();
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

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

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

}

解决方案

Based on your Code. I found the Issues where its getting Crashed. Might be The Instance of your gps is null or is getting changed to null. Check this first

if(gps!=null){
  //your code
}else{
   Log.i("Your Classs Name::","GPS Object is null")
}

I sure you will get Log Message as GPS Object is null..

If still get Null then re-initialize the gps object.

这篇关于如何解决应用程序获得坠毁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆