hi there
could you tell what I am doing wrong in here. the app crashes. FYI, I am beginner.
the permistion for Access_fine_Location is set and all necessary classes are imported.
this is the main class:
and this is my other class:
when I put all the code in the main class it works like a charm!
could you tell what I am doing wrong in here. the app crashes. FYI, I am beginner.
the permistion for Access_fine_Location is set and all necessary classes are imported.
this is the main class:
Code:
public class MainActivity extends Activity{
private Speedometer speedometer;
[USER=1021285]@override[/USER]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
speedometer = new Speedometer();
}
// I guess the problam is here!
[USER=1021285]@override[/USER]
protected void onStart() {
super.onStart();
speedometer.onLocationChanged(null);
}
}
and this is my other class:
Code:
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.Window;
import android.widget.TextView;
public class Speedometer extends Activity implements LocationListener {
[USER=1021285]@override[/USER]
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
this.onLocationChanged(null);
}
[USER=1021285]@override[/USER]
public void onLocationChanged (Location location){
TextView myText = (TextView) this.findViewById(R.id.SpeedId);
if (location == null) {
myText.setText("0");
} else {
float nCurrentSpeed = location.getSpeed();
myText.setText((int) nCurrentSpeed * 18/5 + "");
}
}
when I put all the code in the main class it works like a charm!
Last edited by a moderator: