• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps retrieving data from a class.

abe_ko

Lurker
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:

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:
Hi and welcome to Android Forums.
Please read this thread for some useful information for beginners

http://androidforums.com/threads/please-read-me-before-posting.987318/

If your application crashed, it would have generated a stack trace in the Logcat window. It would be very useful to see that output if you can post it.

Also your code is quite strange, because it's not normal to specifically create an instance of an Activity class. If you wish to start a new Activity, this is normally done using an Intent

https://developer.android.com/training/basics/firstapp/starting-activity.html
 
Back
Top Bottom