在片段内的onLocationChanged内更改TextView时应用程序崩溃

时间:2016-08-21 10:55:45

标签: java android android-fragments textview

我是开发Android的初学者。我在我的应用程序中我有GPS和导航抽屉工作。我的目标是在片段中显示坐标。我有应用程序将GPS数据输入logcat,现在我尝试从TextvView方法更改onLocationChanged上的文字。更改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);

4 个答案:

答案 0 :(得分:0)

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


}

答案 1 :(得分:0)

将您的观点传递给像这样的方法

    public void onLocationChanged(Location location, View view) {

    gpsLongitude = (TextView) view.findViewById(R.id.gps_longitude);
    gpsLongitude.setText("something");

}

当你调用你的方法时,传递视图

onLocationChanged(location,myView);

答案 2 :(得分:0)

首先,请确保在Fragment

中实施android.location.LocationListener的正确界面
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));
    }
}

答案 3 :(得分:-1)

好的,我找到了解决方案:

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 showGPSData (View view) {
    gpsLongitude.setText("Longitude: " + Double.toString(longitude));
}


public void onLocationChanged(Location location) {
    longitude = location.getLongitude();
    showGPSData(myView);
}

基本上我只是创建了一个用于在Text视图中更改文本(showGPSData)的方法,将Text视图移动到onCreate并进行调整以便在位置更改时调用方法(showGPSData)。