我是一个为Android开发的初学者。我在我的应用程序,我有GPS和导航抽屉工作。我的目标是在片段中显示坐标。我得到了将全球定位系统数据输入logcat
的应用程序,现在我正在尝试从onLocationChanged
方法中更改TextvView
上的文本。在onCreateView
中更改文本是完美的。
它的工作方式如下:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.fragment_gps, container, false);
gpsLongitude = (TextView) myView.findViewById(R.id.gps_longitude);
gpsLongitude.setText("something");
return myView;
}
但当我这么做的时候:
private double longitude;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.fragment_gps, container, false);
gpsLongitude = (TextView) myView.findViewById(R.id.gps_longitude);
return myView;
}
...
public void onLocationChanged(Location location) {
Log.e("GPS", "found signal");
longitude = location.getLongitude();
gpsLongitude.setText("Longitude: " + Double.toString(longitude));
}
我得到:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.view.View android.view.View.findViewById(int)‘
(这发生在我得到: E/GPS:在logcat中找到信号之后)
如果我这么做:
public void onLocationChanged(Location location) {
gpsLongitude = (TextView) getView().findViewById(R.id.gps_longitude);
gpsLongitude.setText("Longitude: " + Double.toString(longitude));
}
同样的事情也发生了:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.view.View android.view.View.findViewById(int)‘
这样做:
public void onLocationChanged(Location location) {
gpsLongitude = (TextView) getView().findViewById(R.id.gps_longitude);
gpsLongitude.setText("something");
}
在以下方面的成果:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.view.View android.view.View.findViewById(int)‘
XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/gps_longitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Longitude"
android:textSize="40dp"
android:textAlignment="center"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/gps_latitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Latitude"
android:textSize="40dp"
android:textAlignment="center"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/gps_altitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Altitude"
android:textSize="40dp"
android:textAlignment="center"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/gps_speed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Speed"
android:textSize="40dp"
android:textAlignment="center"
android:layout_margin="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:gravity="center">
<Button
android:onClick="onStartButtonClick"
android:id="@+id/gps_start"
android:layout_width="150dp"
android:layout_height="match_parent"
android:text="Start"
android:textSize="20dp"/>
<Button
android:onClick="onStopButtonClick"
android:id="@+id/gps_stop"
android:layout_width="150dp"
android:layout_height="match_parent"
android:text="Stop"
android:textSize="20dp"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
几个小时以来一直在寻找答案。
编辑:
MainActivity.java:
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
GPSFragment locationListener = new GPSFragment();
if (android.os.Build.VERSION.SDK_INT > 23) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION
}, 10);
return;
}
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);
发布于 2016-08-21 03:15:07
private double longitude;
private ViewGroup myView = null;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.fragment_gps, container, false);
return myView;
}
public void onLocationChanged(Location location) {
Log.e("GPS", "found signal");
if (null != myView) {
TextView gpsLongitude = (TextView) myView.findViewById(R.id.gps_longitude);
longitude = location.getLongitude();
gpsLongitude.setText("Longitude: " + Double.toString(longitude));
}
}
发布于 2016-08-21 03:26:04
将视图传递给这样的方法
public void onLocationChanged(Location location, View view) {
gpsLongitude = (TextView) view.findViewById(R.id.gps_longitude);
gpsLongitude.setText("something");
}
调用方法时,传递视图
onLocationChanged(location,myView);
发布于 2016-08-21 18:00:51
首先,确保您在android.location.LocationListener
中实现了正确的Fragment
接口。
public class GPSFragment extends Fragment implements LocationListener {
...
}
您应该在requestLocationUpdates
中请求onCreateView()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.fragment_gps, container, false);
gpsLongitude = (TextView) myView.findViewById(R.id.gps_longitude);
LocationManager lm= (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, (android.location.LocationListener) this);
return myView;
}
@Override
public void onLocationChanged(Location location) {
if(location==null){
gpsLongitude.setText("Unknown location");
}else {
Log.e("GPS", "found signal");
double longitude = location.getLongitude();
gpsLongitude.setText("Longitude: " + Double.toString(longitude));
}
}
https://stackoverflow.com/questions/39063411
复制