我一直在互联网上搜索,但无法找到问题的解决方案。问题是我无法将我的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个小时,很快我就会失去理智。有什么想法吗?
答案 0 :(得分:2)
ShowGPS locationListener = new ShowGPS();
永远不要在扩展Activity
的类上使用new运算符。使用new运算符时,Activity不会进入其生命周期,onCreate不会被调用,也不会创建View层次结构。更重要的是有效 Context
未附加到您的Activity
,使findViewById无法使用
这一行
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
应该是
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
ShowGPS
已经实现LocationListener
,您不需要为了列表器而实例化它
答案 1 :(得分:0)
在TextView
后Oncreate
内setContentView
初始化textView
并将src_builder
对象作为全局实例。