2012-03-28 68 views
-1

我正在做一個小應用程序,它需要當前的GPS值。爲此,我閱讀了許多教程,並嘗試使用此sample code。但我沒有得到價值。當我運行代碼強行關閉應用程序。這是我的logcat。沒有得到當前的GPS值

03-27 12:57:49.881: E/AndroidRuntime(604): FATAL EXCEPTION: main 
03-27 12:57:49.881: E/AndroidRuntime(604): java.lang.UnsatisfiedLinkError: onCreate 
03-27 12:57:49.881: E/AndroidRuntime(604): at com.google.android.maps.MapActivity.onCreate(Native Method) 
03-27 12:57:49.881: E/AndroidRuntime(604): at com.android.and.AndActivity.onCreate(AndActivity.java:48) 
03-27 12:57:49.881: E/AndroidRuntime(604): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
03-27 12:57:49.881: E/AndroidRuntime(604): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
03-27 12:57:49.881: E/AndroidRuntime(604): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
03-27 12:57:49.881: E/AndroidRuntime(604): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
03-27 12:57:49.881: E/AndroidRuntime(604): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
03-27 12:57:49.881: E/AndroidRuntime(604): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-27 12:57:49.881: E/AndroidRuntime(604): at android.os.Looper.loop(Looper.java:123) 
03-27 12:57:49.881: E/AndroidRuntime(604): at android.app.ActivityThread.main(ActivityThread.java:4627) 
03-27 12:57:49.881: E/AndroidRuntime(604): at java.lang.reflect.Method.invokeNative(Native Method) 
03-27 12:57:49.881: E/AndroidRuntime(604): at java.lang.reflect.Method.invoke(Method.java:521) 
03-27 12:57:49.881: E/AndroidRuntime(604): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
03-27 12:57:49.881: E/AndroidRuntime(604): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
03-27 12:57:49.881: E/AndroidRuntime(604): at dalvik.system.NativeStart.main(Native Method) 

編輯

這裏是我的活動。

public class AndActivity extends Activity implements LocationListener { 
    private TextView latituteField; 
    private TextView longitudeField; 
    private LocationManager locationManager; 
    private String provider; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     latituteField = (TextView) findViewById(R.id.TextView02); 
     longitudeField = (TextView) findViewById(R.id.TextView04); 

     // Get the location manager 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     // Define the criteria how to select the locatioin provider -> use 
     // default 
     Criteria criteria = new Criteria(); 
     provider = locationManager.getBestProvider(criteria, false); 
     Location location = locationManager.getLastKnownLocation(provider); 

     // Initialize the location fields 
     if (location != null) { 
      System.out.println("Provider " + provider + " has been selected."); 
      int lat = (int) (location.getLatitude()); 
      int lng = (int) (location.getLongitude()); 
      latituteField.setText(String.valueOf(lat)); 
      longitudeField.setText(String.valueOf(lng)); 
     } else { 
      latituteField.setText("Provider not available"); 
      longitudeField.setText("Provider not available"); 
     } 
    } 

    /* Request updates at startup */ 
    @Override 
    protected void onResume() { 
     super.onResume(); 
     locationManager.requestLocationUpdates(provider, 400, 1, this); 
    } 

    /* Remove the locationlistener updates when Activity is paused */ 
    @Override 
    protected void onPause() { 
     super.onPause(); 
     locationManager.removeUpdates(this); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 

     int lat = (int) (location.getLatitude()); 
     int lng = (int) (location.getLongitude()); 
     latituteField.setText(String.valueOf(lat)); 
     longitudeField.setText(String.valueOf(lng)); 
    } 

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

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     Toast.makeText(this, "Enabled new provider " + provider, 
       Toast.LENGTH_SHORT).show(); 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     Toast.makeText(this, "Disabled provider " + provider, 
       Toast.LENGTH_SHORT).show(); 
    } 
} 
+0

顯示您在此活動中使用的AndActivity'onCreate(..)'代碼和xml佈局。 – 2012-03-28 07:07:17

+0

@ a.ch我在這裏添加了我的AndActivity .. – wolverine 2012-03-28 07:10:26

+0

哪一行是您的活動中的第48位? – 2012-03-28 07:14:35

回答

0

嗨,我的代碼在我的手機中工作。該代碼在我的模擬器中不起作用。當我在手機上安裝它時,它工作正常。 謝謝你的回覆。

1

獲取自己當前GPS座標的一些有用鏈接。

http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/

http://www.oudmaijer.com/2010/04/15/android-retrieving-your-current-location/

http://developer.android.com/guide/topics/location/obtaining-user-location.html

http://about-android.blogspot.in/2010/04/find-current-location-in-android-gps.html

http://www.android10.org/index.php/articleslocationmaps/209-obtaining-user-location

http://www.android10.org/index.php/articleslocationmaps/226-android-location-providers-gps-network-p

你可以找到更多關於獲取用戶當前GPS座標的簡單明瞭的信息。一些教程也在那裏。希望這會幫助你。

編輯

嘗試......

public class gpsactivity { 

    private static final int gpsMinTime = 6000; 
    private static final int gpsMinDistance = 1; 

    private LocationManager locationManager = null; 
    private LocationListener locationListener = null; 
    private Location location; 
    private String bestProvider; 
    private GPSCallback gpsCallback = null; 
    private GeomagneticField geoField; 

    public gpsactivity() { 
     locationListener = new LocationListener() { 
      public void onProviderDisabled(final String provider) { 
      } 

      public void onProviderEnabled(final String provider) { 
      } 

      public void onLocationChanged(final Location location) { 
       if (location != null && gpsCallback != null) { 
//     gpsCallback.onGPSUpdate(location); 

        geoField = new GeomagneticField(
          Double.valueOf(location.getLatitude()).floatValue(), 
          Double.valueOf(location.getLongitude()).floatValue(), 
          Double.valueOf(location.getAltitude()).floatValue(), 
          System.currentTimeMillis() 
         ); 
       } 
      } 

      @Override 
      public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
       // TODO Auto-generated method stub 
      } 
     }; 
    } 

    public Location getCurrentLocation() { 

     if (locationManager != null && bestProvider != null 
       && bestProvider.length() > 0) { 
      location = locationManager.getLastKnownLocation(bestProvider); 
     } 
     return location; 
    } 

而且調用這個類的

gpsManager = new gpsactivity();    
    location = gpsManager.getCurrentLocation(); 

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


       if (location != null) 
       { 
        location.setLatitude(Latitude); 
        location.setLongitude(Longitude);   
       } 
      } 

      intLatitude = (int) (Latitude * 1000000); 
      intLongitude = (int) (Longitude * 1000000); 

謝謝...

+0

Thak你,我已經嘗試過沒有代碼,但沒有得到輸出。只有當我從模擬器控件發送值時纔會獲得o/p。 – wolverine 2012-03-28 06:50:29

+0

參考我的編輯... – 2012-03-28 06:57:55

0

試試這個 remove從實現LocationListener的 公共類AndActivity擴展活動實現LocationListener的

在這個類來創建單獨的類象 公共類MyLocationListener實現LocationListener的

和實施覆蓋功能。

0

嘿Volworin說的對..

模擬器,我們沒有得到CURENT GPS位置。你必須,應該由你第一次安裝在手機應用程序..

由在GPS狀態電話。

請點擊此鏈接.. click link

我已經嘗試過了,在Android手機。它的工作進行測試。

祝您有美好的一天。