2013-03-16 95 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仿真器等应用程序,也很容易虚拟设备上设置的位置。