im trying to build a simple GPS app that can give the user his current location, his speed, his heading and if he is or isn't moving.

after im declaring the textView's and change their value once i cant seem to change them again and keep getting this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTextColor(int)' on a null object reference

this is my code:

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);
}

}

i have already tried to change the declaration space and the sequence in which the program runs and changes the text in the TextView.

i know i can simply make the TextView text be what i want it to be when the GPS does not give a location Change but i still want to understand what i did wrong

im sure its a roocky mistake and its probably some java lang knowledge gap. thanks in advance

1

There are 1 answers

0
peshkira On BEST ANSWER

move the lines

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

as the first line in the onCreate method.

The problem ist that you try to find the text views before telling android which xml file to inflate. That is why the findViewById methods return null.

So your method should look like this:

@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);

  ...
}