首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当TextView在片段内的onLocationChanged中更改时,应用程序崩溃

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

Stack Overflow用户
提问于 2016-08-21 10:55:44
回答 4查看 286关注 0票数 1

我是一个为Android开发的初学者。我在我的应用程序,我有GPS和导航抽屉工作。我的目标是在片段中显示坐标。我得到了将全球定位系统数据输入logcat的应用程序,现在我正在尝试从onLocationChanged方法中更改TextvView上的文本。在onCreateView中更改文本是完美的。

它的工作方式如下:

代码语言:javascript
运行
复制
@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;
}

但当我这么做的时候:

代码语言:javascript
运行
复制
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中找到信号之后)

如果我这么做:

代码语言:javascript
运行
复制
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)‘

这样做:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
<?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:

代码语言:javascript
运行
复制
    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);
EN

回答 4

Stack Overflow用户

发布于 2016-08-21 11:15:07

代码语言:javascript
运行
复制
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));
    }


}
票数 0
EN

Stack Overflow用户

发布于 2016-08-21 11:26:04

将视图传递给这样的方法

代码语言:javascript
运行
复制
    public void onLocationChanged(Location location, View view) {

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

}

调用方法时,传递视图

代码语言:javascript
运行
复制
onLocationChanged(location,myView);
票数 0
EN

Stack Overflow用户

发布于 2016-08-22 02:00:51

首先,确保您在android.location.LocationListener中实现了正确的Fragment接口。

代码语言:javascript
运行
复制
public class GPSFragment extends Fragment implements LocationListener {
...
}

您应该在requestLocationUpdates中请求onCreateView()

代码语言:javascript
运行
复制
@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));
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39063411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档