2012-02-29 61 views
2

我正在做的是試圖獲得一個基本的GPS工作,並不能找出問題(沒有出現錯誤)。當我在模擬器上運行它時崩潰。我使用的Android 2.2GPS不工作

package Weather.app; 


import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.Geocoder; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.Toast; 


public class WeatherAppActivity extends Activity{ 
/** Called when the activity is first created. */ 
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters 
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds 


protected LocationManager locationManager; 
protected LocationListener MyLocationListener; 
protected Button findButton; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    findButton = (Button) findViewById(R.id.findButton); 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(
       locationManager.GPS_PROVIDER, 
       MINIMUM_TIME_BETWEEN_UPDATES, 
       MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, 
       (LocationListener) this 
      ); 



    findButton.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         showCurrentLocation(); 
        } 
    }); 
} 

    protected void showCurrentLocation() { 

       Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

       if (location != null) { 
        String message = String.format(
          "Current Location \n Longitude: %1$s \n Latitude: %2$s", 
          location.getLongitude(), location.getLatitude() 
        ); 
        Toast.makeText(WeatherAppActivity.this, message, 
          Toast.LENGTH_LONG).show(); 

       } 

       final class MyLocationListener implements LocationListener { 
        @Override 
         public void onLocationChanged(Location location) { 
          String message = String.format(
           "New Location \n Longitude: %1$s \n Latitude: %2$s", 
          location.getLongitude(), location.getLatitude() 
          ); 
           Toast.makeText(WeatherAppActivity.this, message, Toast.LENGTH_LONG).show(); 
         } 
        @Override 
         public void onStatusChanged(String s, int i, Bundle b) { 
           Toast.makeText(WeatherAppActivity.this, "Provider status changed", 
            Toast.LENGTH_LONG).show(); 
          } 
        @Override 
         public void onProviderDisabled(String s) { 
          Toast.makeText(WeatherAppActivity.this, 
            "Provider disabled by the user. GPS turned off", 
             Toast.LENGTH_LONG).show(); 
          } 
        @Override 
         public void onProviderEnabled(String s) { 
          Toast.makeText(WeatherAppActivity.this, 
            "Provider enabled by the user. GPS turned on", 
             Toast.LENGTH_LONG).show(); 
          } 

       }       






} 

}

這也是我的清單:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> 
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" /> 

我已經更新了這個代碼,我已經做了一些改動

+0

發佈logcat,顯示應用程序崩潰的位置。有了這個,我們可以幫助精確定位它崩潰的原因,以及爲什麼 – dymmeh 2012-02-29 19:41:31

+0

你在哪裏有<使用權限android:name =「android.permission.ACCESS_FINE_LOCATION」/>'?在後面試試' – JanOlMajti 2012-02-29 19:49:01

+0

是的,它在下,它在logcat中顯示的是E/AndroidRuntime(340):\t ... 11 more – user1234167 2012-03-02 20:28:12

回答

2

這裏是工作的基本的GPS活動的一個例子:

public class UseGpsActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    LocationListener mlocListener = new MyLocationListener(); 

    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 

    } 
    /* Class My Location Listener */ 

public class MyLocationListener implements LocationListener{ 

    @Override 
    public void onLocationChanged(Location loc) { 
     // TODO Auto-generated method stub 
     double lati = loc.getLatitude(); 
     double longi = loc.getLongitude(); 

     String Text = "My current location is: " + "Latitud = " + lati + "Longitud = " + longi; 
     Toast.makeText(getApplicationContext(),Text,Toast.LENGTH_SHORT).show(); 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 
     Toast.makeText(getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT).show(); 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 
     Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show(); 

    } 

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

    } 
    } 
} 
0

除非是一些代碼在你的文章MyLocationListener(你不應該使用用戶實例字段的大寫標識符!)在通過引用時沒有被初始化(即null)至requestLocationUpdates()

0

你並不需要創建另一個LocationListener的,聲明

WeatherAppActivity extends Activity implements LocationListener 

和重寫的方法

@Override 
public void onLocationChanged(Location location) 

等意味着你已經有一個在你的主類,所以擺脫內部類MyLocationListener完全。

將您的Toast代碼等放入主類中的重寫偵聽器方法內。

然後在的onCreate()有:

locationManager.requestLocationUpdates(
    locationManager.GPS_PROVIDER, 
    MINIMUM_TIME_BETWEEN_UPDATES, 
    MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, 
    this 
);