2012-03-03 115 views
0

什麼我做的是試圖得到一個基本的GPS工作和logcat的說:GPS應用程序崩潰

03-04 17:06:37.052: E/AndroidRuntime(278): FATAL EXCEPTION: main 
03-04 17:06:37.052: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to start activity ComponentInfo{Weather.app/Weather.app.WeatherAppActivity}: java.lang.IllegalArgumentException: listener==null 
03-04 17:06:37.052: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
03-04 17:06:37.052: E/AndroidRuntime(278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
03-04 17:06:37.052: E/AndroidRuntime(278): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
03-04 17:06:37.052: E/AndroidRuntime(278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
03-04 17:06:37.052: E/AndroidRuntime(278): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-04 17:06:37.052: E/AndroidRuntime(278): at android.os.Looper.loop(Looper.java:123) 
03-04 17:06:37.052: E/AndroidRuntime(278): at android.app.ActivityThread.main(ActivityThread.java:4627) 
03-04 17:06:37.052: E/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method) 
03-04 17:06:37.052: E/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521) 
03-04 17:06:37.052: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
03-04 17:06:37.052: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
03-04 17:06:37.052: E/AndroidRuntime(278): at dalvik.system.NativeStart.main(Native Method) 
03-04 17:06:37.052: E/AndroidRuntime(278): Caused by: java.lang.IllegalArgumentException: listener==null 
03-04 17:06:37.052: E/AndroidRuntime(278): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:628) 
03-04 17:06:37.052: E/AndroidRuntime(278): at Weather.app.WeatherAppActivity.onCreate(WeatherAppActivity.java:36) 
03-04 17:06:37.052: E/AndroidRuntime(278): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
03-04 17:06:37.052: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
03-04 17:06:37.052: E/AndroidRuntime(278): ... 11 more 


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 
static final String tag = "Main"; // for Log 

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

    @Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    LinearLayout ll = new LinearLayout(this); 
    findButton = new Button(this); 
    ll.addView(findButton); 
    setContentView(ll); 
    locationListener = new MyLocationListener(); 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(
      LocationManager.GPS_PROVIDER, 0, 0, locationListener 
      ); 



    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 provider, int status, Bundle extras) { 
          /* This is called when the GPS status alters */ 
          switch (status) { 
          case LocationProvider.OUT_OF_SERVICE: 
           Log.v(tag, "Status Changed: Out of Service"); 
           Toast.makeText(WeatherAppActivity.this, "Status Changed: Out of Service", 
             Toast.LENGTH_SHORT).show(); 
           break; 
          case LocationProvider.TEMPORARILY_UNAVAILABLE: 
           Log.v(tag, "Status Changed: Temporarily Unavailable"); 
           Toast.makeText(WeatherAppActivity.this, "Status Changed: Temporarily Unavailable", 
             Toast.LENGTH_SHORT).show(); 
           break; 
          case LocationProvider.AVAILABLE: 
           Log.v(tag, "Status Changed: Available"); 
           Toast.makeText(WeatherAppActivity.this, "Status Changed: Available", 
             Toast.LENGTH_SHORT).show(); 
           break; 
          } 
         } 
        @Override 
         public void onProviderDisabled(String provider) { 
          Toast.makeText(WeatherAppActivity.this, 
            "Provider disabled by the user. GPS turned off", 
             Toast.LENGTH_LONG).show(); 
          } 
        @Override 
         public void onProviderEnabled(String provider) { 
          Toast.makeText(WeatherAppActivity.this, 
            "Provider enabled by the user. GPS turned on", 
             Toast.LENGTH_LONG).show(); 
          } 


       }       






} 
} 

這也是我的清單:

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
    <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

editted as我改變了代碼新的錯誤。在線上也發生錯誤:locationListener = new MyLocationListener();錯誤是:MyLocationListener無法解析爲類型 – user1234167 2012-03-04 18:03:40

回答

0

試試這個變種。我剛剛在手機上進行了測試,未發生故障。 權限似乎與您一致。

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 
static final String tag = "Main"; // for Log 

protected LocationManager locationManager; 
protected LocationListener locationListener; 
protected Button findButton; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    LinearLayout ll = new LinearLayout(this); 
    findButton = new Button(this); 
    findButton.setText("findMeh"); 
    ll.addView(findButton); 
    setContentView(ll); 
    locationListener = new MyLocationListener(); 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(
      LocationManager.GPS_PROVIDER, 0, 0, locationListener 
      ); 



    findButton.setOnClickListener(new OnClickListener() { 

        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(); 

       } 

    } 

     class MyLocationListener implements LocationListener { 

         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(); 
         } 

         public void onStatusChanged(String provider, int status, Bundle extras) { 
          /* This is called when the GPS status alters */ 
          switch (status) { 
          case LocationProvider.OUT_OF_SERVICE: 
           Log.v(tag, "Status Changed: Out of Service"); 
           Toast.makeText(WeatherAppActivity.this, "Status Changed: Out of Service", 
             Toast.LENGTH_SHORT).show(); 
           break; 
          case LocationProvider.TEMPORARILY_UNAVAILABLE: 
           Log.v(tag, "Status Changed: Temporarily Unavailable"); 
           Toast.makeText(WeatherAppActivity.this, "Status Changed: Temporarily Unavailable", 
             Toast.LENGTH_SHORT).show(); 
           break; 
          case LocationProvider.AVAILABLE: 
           Log.v(tag, "Status Changed: Available"); 
           Toast.makeText(WeatherAppActivity.this, "Status Changed: Available", 
             Toast.LENGTH_SHORT).show(); 
           break; 
          } 
         } 

         public void onProviderDisabled(String provider) { 
          Toast.makeText(WeatherAppActivity.this, 
            "Provider disabled by the user. GPS turned off", 
             Toast.LENGTH_LONG).show(); 
          } 

         public void onProviderEnabled(String provider) { 
          Toast.makeText(WeatherAppActivity.this, 
            "Provider enabled by the user. GPS turned on", 
             Toast.LENGTH_LONG).show(); 
          } 


       } 

} 
+0

我也使用'LocationManager.NETWORK_PROVIDER,0,0,locationListener'進行測試,因爲我在建築物內部,它向我顯示Toast消息正常。 – 2012-03-03 20:48:49