[英]App crashing when TextView is changed inside onLocationChanged inside a fragment
我是為Android開發的初學者。 我在我的應用程序中啟用了GPS和導航抽屜。 我的目標是在片段中顯示坐標。 我得到了將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);
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));
}
}
將您的視圖傳遞給這樣的方法
public void onLocationChanged(Location location, View view) {
gpsLongitude = (TextView) view.findViewById(R.id.gps_longitude);
gpsLongitude.setText("something");
}
當您調用方法時,通過視圖
onLocationChanged(location,myView);
首先,請確保您在Fragment
實現android.location.LocationListener
的正確接口
public class GPSFragment extends Fragment implements LocationListener {
...
}
您應該在onCreateView()
請求requestLocationUpdates
@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));
}
}
好的,所以我找到了一個解決方案:
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);
}
因此,從本質上講,我只是創建了一種旨在在“文本”視圖中更改文本(showGPSData)的方法,將“文本”視圖移至onCreate並進行了構建,以便在位置更改時調用該方法(showGPSData)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.