2013-03-16 117 views
0

我正在嘗試此位置的代碼,但是當我運行模擬器它說「不幸的位置已停止」...任何想法? 這對於MainAcivity 代碼(我跟着這個教程http://www.vogella.com/articles/AndroidLocationAPI/article.html#locationapi_gpsenable在模擬器上的Android位置

public class MainActivity 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."); 
      onLocationChanged(location); 
     } else { 
      latituteField.setText("Location not available"); 
      longitudeField.setText("Location 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) { 
     // TODO Auto-generated method stub 

     } 

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

回答

2

試試這個代碼我可能會幫助你..

http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

還設置geoposition 可以使用Eclipse的「DDMS」透視圖將您的地理位置發送到仿真器或連接的設備。要打開此透視圖,請選擇窗口→打開透視圖→其他...→DDMS。

在模擬器控制部分,您可以輸入地理座標並按發送按鈕。

enter image description here

2

您必須手動輸入您的仿真器的位置。有幾個選項

方法1:使用DDMS

  • 去你的Android/tools目錄,並啓動DDMS工具

  • 選擇模擬器控制選項卡

  • 用經度和緯度填充位置控制字段 值

  • 按發送按鈕

選項2:使用telnet

  • 打開命令殼
  • CONECT與命令仿真器:遠程登錄本地主機
  • 輸入固定位置執行命令:

地理修復<經度> <緯度> [<海拔>]

方案3:使用第三方應用程序

你可以嘗試android-gps-emulator應用設置的位置。有可能是這樣的

來源

1

您可以使用Genymotion仿真器等應用程序,也很容易虛擬設備上設置的位置。