java.lang.NullPointerException:尝试在空对象引用上调用虚方法 'void android.widget.TextView.setTextColor(int)'
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTextColor(int)' on a null object reference
我正在尝试构建一个简单的 GPS 应用程序,可以为用户提供他的当前位置、他的速度、他的航向以及他是否在移动。
在我声明 textView 并更改它们的值后,我似乎无法再次更改它们并不断收到此错误:
java.lang.NullPointerException: 尝试在空对象引用
上调用虚拟方法'void android.widget.TextView.setTextColor(int)'
这是我的代码:
package com.example.barel.navigationaid;
import android.content.Context;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
final TextView userSpeed = (TextView) findViewById(R.id.userSpeed);
final TextView userLocation = (TextView) findViewById(R.id.userLocation);
final TextView isMoving = (TextView) findViewById(R.id.isMoving);
final TextView userHeading = (TextView) findViewById(R.id.userHeading);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
String locationProvider = locationManager.NETWORK_PROVIDER;
Location locObject = new Location(locationProvider);
userSpeed.setTextColor(Color.RED);
isMoving.setTextColor(Color.RED);
userSpeed.setText("Stationery");
isMoving.setText("Not Moving");
userHeading.setTextColor(Color.RED);
userHeading.setText("No Bearing");
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
userLocation.setText("Long: " + location.getLongitude() + " Lat:" + location.getLatitude());
if(location.hasSpeed()) {
userSpeed.setTextColor(Color.GREEN);
isMoving.setTextColor(Color.GREEN);
userSpeed.setText("Speed: " + location.getSpeed());
isMoving.setText("Moving");
}
if(location.hasBearing()) {
userHeading.setTextColor(Color.GREEN);
userHeading.setText("Heading: " + location.getBearing());
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
userHeading.setText("*****");
userSpeed.setText("*****");
userLocation.setTextColor(Color.RED);
userLocation.setText("Gps Off");
isMoving.setText("*****");
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0,locationListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我已经尝试更改声明 space 和程序运行的顺序并更改 TextView 中的文本。
我知道当 GPS 没有给出位置更改时,我可以简单地使 TextView 文本成为我想要的文本,但我仍然想了解我做错了什么
我确定这是一个愚蠢的错误,并且可能存在一些 java 语言知识差距。
提前致谢
移动行
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
作为 onCreate 方法的第一行。
问题是您试图在告诉 android 扩充哪个 xml 文件之前找到文本视图。这就是为什么 findViewById 方法 return null.
所以你的方法应该是这样的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView userSpeed = (TextView) findViewById(R.id.userSpeed);
final TextView userLocation = (TextView) findViewById(R.id.userLocation);
final TextView isMoving = (TextView) findViewById(R.id.isMoving);
final TextView userHeading = (TextView) findViewById(R.id.userHeading);
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
...
}
我正在尝试构建一个简单的 GPS 应用程序,可以为用户提供他的当前位置、他的速度、他的航向以及他是否在移动。
在我声明 textView 并更改它们的值后,我似乎无法再次更改它们并不断收到此错误:
java.lang.NullPointerException: 尝试在空对象引用
上调用虚拟方法'void android.widget.TextView.setTextColor(int)'这是我的代码:
package com.example.barel.navigationaid;
import android.content.Context;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
final TextView userSpeed = (TextView) findViewById(R.id.userSpeed);
final TextView userLocation = (TextView) findViewById(R.id.userLocation);
final TextView isMoving = (TextView) findViewById(R.id.isMoving);
final TextView userHeading = (TextView) findViewById(R.id.userHeading);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
String locationProvider = locationManager.NETWORK_PROVIDER;
Location locObject = new Location(locationProvider);
userSpeed.setTextColor(Color.RED);
isMoving.setTextColor(Color.RED);
userSpeed.setText("Stationery");
isMoving.setText("Not Moving");
userHeading.setTextColor(Color.RED);
userHeading.setText("No Bearing");
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
userLocation.setText("Long: " + location.getLongitude() + " Lat:" + location.getLatitude());
if(location.hasSpeed()) {
userSpeed.setTextColor(Color.GREEN);
isMoving.setTextColor(Color.GREEN);
userSpeed.setText("Speed: " + location.getSpeed());
isMoving.setText("Moving");
}
if(location.hasBearing()) {
userHeading.setTextColor(Color.GREEN);
userHeading.setText("Heading: " + location.getBearing());
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
userHeading.setText("*****");
userSpeed.setText("*****");
userLocation.setTextColor(Color.RED);
userLocation.setText("Gps Off");
isMoving.setText("*****");
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0,locationListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我已经尝试更改声明 space 和程序运行的顺序并更改 TextView 中的文本。
我知道当 GPS 没有给出位置更改时,我可以简单地使 TextView 文本成为我想要的文本,但我仍然想了解我做错了什么
我确定这是一个愚蠢的错误,并且可能存在一些 java 语言知识差距。 提前致谢
移动行
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
作为 onCreate 方法的第一行。
问题是您试图在告诉 android 扩充哪个 xml 文件之前找到文本视图。这就是为什么 findViewById 方法 return null.
所以你的方法应该是这样的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView userSpeed = (TextView) findViewById(R.id.userSpeed);
final TextView userLocation = (TextView) findViewById(R.id.userLocation);
final TextView isMoving = (TextView) findViewById(R.id.isMoving);
final TextView userHeading = (TextView) findViewById(R.id.userHeading);
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
...
}