java.lang.NullPointerException:尝试在空对象引用

时间:2015-06-10 21:08:46

标签: android gps

我试图建立一个简单的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);
}

}

我已经尝试更改声明空间和程序运行的顺序,并更改TextView中的文本。

我知道我可以简单地让TextView文本成为我想要它的时候GPS没有给出位置改变但我还是想了解我做错了什么

我确定这是一个错误的错误,可能还有一些java知识差距。 提前谢谢

1 个答案:

答案 0 :(得分:0)

移动线

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

作为onCreate方法的第一行。

问题是你在告诉android哪个xml文件要膨胀之前试图找到文本视图。这就是findViewById方法返回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);

  ...
}