2014-12-07 74 views
-3

當我試圖從GPS提供者獲取coordinatess時,出現此錯誤。嘗試獲取協調器時發生StackOverFlow錯誤

package com.example.task1; 

public class GPSTracker extends Service implements LocationListener 
{ 
    private Context Mycontext; 

    boolean IsGPSEnabled = false; 
    boolean IsNetworkEnabled = false; 
    boolean CanGetLocation = false; 

    Location location; 
    double Latitude; 
    double Longitude; 

    private static final int MinDstance = 0; // minimum distance to change 
               // update in meters 
    private static final long MinTime = 0;// minimum time between update in 
              // milisec 

    protected LocationManager LocationMgr; 

    public void GPSTracker1(Context context, TextView vieww) 
    { 
     this.Mycontext = context; 
     GetLocation(vieww); 
    } 

    public Location GetLocation(TextView view) 
    {  
     try { 
      LocationMgr = (LocationManager) Mycontext 
        .getSystemService(LOCATION_SERVICE); 

      IsGPSEnabled = LocationMgr 
        .isProviderEnabled(LocationManager.GPS_PROVIDER); 
      IsNetworkEnabled = LocationMgr 
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

      if (!IsGPSEnabled && !IsNetworkEnabled) { 

       view.setText("No network availables"); 

      } else { 
       this.CanGetLocation = true; 

       if (IsNetworkEnabled) { 
        LocationMgr.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, MinDstance, 
          MinTime, this); 

        Log.d("Network", "Network"); 

        if (LocationMgr != null) { 
         location = LocationMgr 
           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         updateGPSCoordinator(); 
        } 
       } 

       if (IsGPSEnabled) { 
        if (location == null) { 
         LocationMgr.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, MinDstance, 
           MinTime, this); 
         Log.d("GPS enabled", "GPS Enalbed"); 

         if (LocationMgr != null) { 
          location = LocationMgr 
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          updateGPSCoordinator(); 

         } 
        } 
       } 
      } 

      /* 
      * if(location != null) { Latitude = location.getLatitude(); 
      * Longitude = location.getLongitude(); 
      * 
      * 
      * } 
      */ 

     } catch (StackOverflowError e) { 
      Log.e("Error : Location", 
        "Impossible to connect to LocationManager", e); 

     } 

     return location; 

    } 

    public void updateGPSCoordinator() 
    { 
     if (location != null) { 
      Latitude = location.getLatitude(); 
      Longitude = location.getLongitude(); 
     } 
    } 

    public void stopUsingGPS() 
    { 
     if (LocationMgr != null) { 

      LocationMgr.removeUpdates(GPSTracker.this); 
     } 
    } 

    public double getLatitude() 
    { 

     if (location != null) { 
      Latitude = location.getLatitude(); 

     } 
     return Latitude; 
    } 

    public double getLongitude() { 

     if (location != null) { 
      Longitude = location.getLongitude(); 
     } 

     return Longitude; 
    } 

    boolean canGetlocation() { 
     return this.canGetlocation(); 

    } 

    public void showSettingsAlert() { 

     AlertDialog.Builder AlertDialog = new AlertDialog.Builder(Mycontext); 

     AlertDialog.setTitle("GPS IS OFF"); 
     AlertDialog 
       .setMessage("GPS Is Not enabled Do you want to go to settings"); 

     AlertDialog.setPositiveButton("Settings", 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 

         Intent intent = new Intent(
           android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
         Mycontext.startActivity(intent); 

        } 
       }); 

     AlertDialog.setNegativeButton("Cancel", 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 

        } 
       }); 

     AlertDialog.show(); 
    } 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 

} 

,這裏是在MainActivity代碼:

package com.example.task1; 

public class Main_Page extends Activity { 

    Button Run_btn; 
    TextView View_txt; 
    GPSTracker gps; 

    ProgressBar pb; 
    List<MyTasks> tasks; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.main_page); 

     View_txt = (TextView) findViewById(R.id.Long_lat_txt); 
     Run_btn = (Button) findViewById(R.id.Run_btn); 

     tasks = new ArrayList(); 

     Run_btn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       gps = new GPSTracker(); 

       gps.GPSTracker1(Main_Page.this, View_txt); 

       if (IsOnline()) { 
        RequestData(); 
       } else { 

        View_txt.setText("No network available"); 

       } 

       if (gps.canGetlocation()) { 

        double latitude = gps.getLatitude(); 
        double longtude = gps.getLongitude(); 

        View_txt.setText("current location Is" + latitude + " " 
          + longtude); 

       } else { 
        gps.showSettingsAlert(); 
       } 

      } 
     }); 

    } 

    protected void updatedisplay(String message) { 
     View_txt.append(message + "\n"); 

    } 

    private void RequestData() { 
     MyTasks Task = new MyTasks(); 
     Task.execute("P 1 ", "P 2 ", "P 3 "); 
    } 

    protected boolean IsOnline() { 
     ConnectivityManager ConMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo NetInfo = ConMgr.getActiveNetworkInfo(); 
     if (NetInfo != null && NetInfo.isConnectedOrConnecting()) { 
      return true; 

     } else { 
      return false; 
     } 

    } 

    private class MyTasks extends AsyncTask<String, String, String> { 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 

     } 

     @Override 
     protected String doInBackground(String... params) { 

      for (int i = 0; i < params.length; i++) { 
       publishProgress("Work With" + params[i]); 

       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      } 
      return "Task complete"; 

     } 

     @Override 
     protected void onProgressUpdate(String... values) { 
      updatedisplay(values[0]); 

     } 
    } 

} 

這裏是記錄錯誤:

12-07 11:18:37.581: E/AndroidRuntime(517): FATAL EXCEPTION: main 
12-07 11:18:37.581: E/AndroidRuntime(517): java.lang.StackOverflowError 
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179) 
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179) 
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179) 
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179) 
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179) 
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179) 
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179) 
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179) 
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179) 
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179) 
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179) 
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179) 

回答

3

你這裏有無限遞歸:

boolean canGetlocation() 
{ 
    return this.canGetlocation(); 
} 

改變它到:

boolean canGetlocation() 
{ 
    return this.CanGetLocation; 
} 

順便說一句,你應該調用你的實例變量和方法canGetLocation尊重Java的命名約定。

+0

非常感謝,但我想讓你知道我是Android的初學者,我想從其他經驗中提高我的技能 – BOB91 2014-12-07 14:59:01

相關問題