0

Im trying to create a layout that will show the received location updates in the text view, but i keep getting this error, what am i doing wrong?

Help would be appreciated, thanks in advance!

Here is the error:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.defro.app, PID: 7731 java.lang.NullPointerException: Attempt to invoke virtual >method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null >object reference at com.example.defro.app.MainActivity$1.onLocationChanged(MainActivity.java:29)`

Row 29 is:

txtLoc.setText(String.valueOf(iaLocation.getLongitude() + ", " + iaLocation.getLatitude()));

Main_Activity:

public class MainActivity extends AppCompatActivity {

IALocationManager mLocationManager;
IALocationListener mLocationListener = new IALocationListener() {
    @Override
    public void onLocationChanged(IALocation iaLocation) {
        TextView txtLoc = (TextView) findViewById(R.id.textView);
        txtLoc.setText(String.valueOf(iaLocation.getLongitude() + ", " + iaLocation.getLatitude()));

    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

};
private final int CODE_PERMISSIONS = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mLocationManager = IALocationManager.create(this);


    String[] neededPermissions = {
            Manifest.permission.CHANGE_WIFI_STATE,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.BLUETOOTH,
            Manifest.permission.BLUETOOTH_ADMIN



    };
    ActivityCompat.requestPermissions(this, neededPermissions, CODE_PERMISSIONS);


}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    //Handle if any of the permissions are denied, in grantResults
}


protected void onResume() {
    super.onResume();
    mLocationManager.requestLocationUpdates(IALocationRequest.create(), mLocationListener);

}

protected void onPause() {
    mLocationManager.removeLocationUpdates(mLocationListener);
    super.onPause();
}

protected void onDestroy() {

   mLocationManager.destroy();
   super.onDestroy();
}

}

content_main.xml:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="23dp"
    android:text="Lat, Long"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.051"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.032" />

Jed
  • 27
  • 4

3 Answers3

2

Write this line inside the onCreate method

TextView txtLoc = (TextView) findViewById(R.id.textView);

Add id to TextView

android:id="@+id/textView"
Huzaifa Iftikhar
  • 755
  • 1
  • 6
  • 9
1

You are linking up your TextView as TextView txtLoc = (TextView) findViewById(R.id.textView);

However there is no ID defined in your XML layout file. Define your Textview id in content_main.xml like android:id="@+id/textView"

Masoom Badi
  • 986
  • 9
  • 18
1

At first glance, the textView does not seem to have the

android:id="@+id/textView"

that may be y its failing to find the view. Additionally, you probably want to get the reference to the textView in the onCreate() and use it along with a null check just incase.

Xenolion
  • 12,035
  • 7
  • 33
  • 48
ariochdivij666
  • 396
  • 3
  • 8