我一直在 Internet 上搜索,但找不到解决我的问题的方法。问题是我无法将我的 GPS 坐标插入到 TextView 中。应用程序在此行崩溃
TextView latText = (TextView) findViewById(R.id.Latitude);
我设置了
setContentView(R.layout.activity_show_gps);
在尝试查找 ViewById 之前,但它仍然不起作用。我在那个 TextView 上也有 id,所以这不是问题。
这里是完整代码:
package com.example.user1.gpsgpsgps;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class ShowGPS extends AppCompatActivity implements LocationListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_gps);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
ShowGPS locationListener = new ShowGPS();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
@Override
public void onLocationChanged(Location location) {
double lat = location.getLatitude();
double lon = location.getLongitude();
TextView latText = (TextView) findViewById(R.id.Latitude);
TextView lonText = (TextView) findViewById(R.id.Longitude);
latText.setText("Nothing works...");
lonText.setText("Nothing works here either...");
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
这里是我的 activity_show_gps.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.markus.gps.GPSActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Latitude"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lon"
android:id="@+id/Longitude"
android:layout_below="@+id/Latitude"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/acc"
android:id="@+id/Accuracy"
android:layout_below="@+id/Longitude"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/alt"
android:id="@+id/Altitude"
android:layout_below="@+id/Accuracy"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/time"
android:id="@+id/Time"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/Altitude" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/refreshGPS"
android:id="@+id/RefreshGPSButton"
android:layout_marginTop="34dp"
android:layout_below="@+id/Time"
android:layout_centerHorizontal="true"
android:onClick="onRefreshGPSButtonClick" />
</RelativeLayout>
这就是我启动 GPS 的方式(主要):
public void onGPSButtonClick(View view) {
Intent intent = new Intent(this, ShowGPS.class);
startActivity(intent);
}
我已经尝试完成这项工作近 6 个小时了,很快我就会失去理智。有什么想法吗?
最佳答案
ShowGPS locationListener = new ShowGPS();
切勿在扩展 Activity
的类上使用 new 运算符。使用 new 运算符,Activity 不会经历其生命周期,不会调用 onCreate 并且您不会创建 View 层次结构。更重要的是 valid Context
没有附加到您的 Activity
,导致 findViewById 无法使用
这一行
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
应该是
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
ShowGPS
已经实现了 LocationListener
,你不需要为了监听器而实例化它
关于java - Android Studio 在 findViewById 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33994134/