我正在学习一个教程,但我遇到了一些代码。问题是我无法将当前坐标输入到我的 textView(id:red) 中,并且它不会改变状态。我怀疑这个错误onLocationChanged()
是没有调用该方法。我还附上了我的 Java 程序和我的布局 xml 文件。感谢您的所有帮助!
Android MainActivity.java
package com.example.wepicit;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.content.Context;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;
import com.facebook.*;
import com.facebook.model.*;
public class MainActivity extends Activity implements LocationListener{
protected LocationManager locationManager;
protected LocationListener locationListener;
Location newLocation = new Location(LocationManager.GPS_PROVIDER);
protected Context context;
String lat;
String provider;
protected String gps_enabled,network_enabled;
Button button;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
newLocation.set(location);
//System.out.println("Lat:"+ location.getLatitude() +" Long:"+ location.getLongitude());
text = (TextView) findViewById(R.id.red);
text.setText("Lat:"+ newLocation.getLatitude() +" Long:"+ newLocation.getLongitude());
Log.d("Lat:"+ newLocation.getLatitude() +" Long:"+ newLocation.getLongitude(), gps_enabled);
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
Log.d("Latitude","disable");
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
Log.d("Latitude","enable");
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
Log.d("Latitude","status");
}
}
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:text="Tester" />
<TextView
android:id="@+id/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:text="TextView" />
</RelativeLayout>