1

我正在尝试在 a 中显示纬度和经度值TextView,但是在运行项目时不显示这些值。任何人都可以帮助我吗?,提前谢谢你。我的代码如下:
GeocodingActivity.class

public class GeocodingActivity extends Activity {
LocationManager locman;
Criteria cri;
LocationProvider locpro;
AlertDialog.Builder builder;
Handler handler;
TextView t1;
LocationListener loclis;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    t1=(TextView)findViewById(R.id.textView1);
    locman=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
       handler=new Handler(){
        public void handle(Message msg){
                  switch(msg.what){
        case 1: 
            t1.setText((String)msg.obj);
            break;
        }

        }
loclis=new LocationListener(){

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
Message.obtain(handler, 1,   arg0.getLatitude()+","+arg0.getLongitude()).sendToTarget();

}
@Override
public void onProviderDisabled(String provider) {
}

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


}

}
4

2 回答 2

2
Message.obtain(handler,0,location.getLatitude()+","+location.getLongitude()).sendToTarget();

所以你必须意识到方法getLongitudegetLatitude返回Double。而你需要String. 所以你需要将它们转换为Stringwith 方法String.valueOf()

Message.obtain(handler, 0, String.valueOf(location.getLatitude()) + "," + String.valueOf(location.getLongitude())).sendToTarget();

然后,Message.obj返回Objectso 以便String从那里得到你应该将它转换为Stringsot1.setText((String) msg.obj);

现在,它应该可以工作了。


编辑:

第二种方法

@Override
public void onLocationChanged(Location location) {
   Bundle bundle = new Bundle();
   String data = String.valueOf(location.getLatitude()) + "," + String.valueOf(location.getLongitude());
   bundle.putString("updateGpsData", data);
   yourMessage.setData(bundle);
   yourMessage.obtain(handler, 0).sendToTarget();

}

然后你的handle方法看起来像这样

public void handle(Message msg){
   t1.setText(msg.getData().getString("updateGpsData"));
}

因此,您可以创建Bundle和更新数据,只需添加到您的数据,Bundle然后在您调用的句柄方法中setData()设置数据以检索更新数据。MessagegetData().getString()GPS

不要忘记这Bundle适用于对键 + 值,因此您Bundle使用特定键将数据设置为您的数据,因此要获取数据,您必须使用相同的键(“updateGpsData”)。

于 2012-06-13T14:03:07.883 回答
0

请像这样在您的坐标上使用该String.valueOf方法

String.valueO(location.getLatitude())+","+String.valueOf(location.getLongitude())
于 2012-06-13T13:47:04.090 回答