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" />