Bob21
Bob21

Reputation: 167

GPS app - Runs but shuts down on button click

I'm making a GPS app where you can get the GPS coordinates by hitting a button. As stated in the title it shuts down when I click the button.

I'm new to Android development and have got this far from my little knowledge of Java and following some Android tutorials.

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements LocationListener {

    private TextView startLatitudeField;
    private TextView startLongitudeField;
    private TextView endLatitudeField;
    private TextView endLongitudeField;
    private LocationManager locationManager;
    private LocationListener locationListener;
    private String provider;
    double startLat = 0;
    double startLng = 0;
    double endLat = 0;
    double endLng = 0;



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

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //Setting TextViews for the coordinates
        startLatitudeField = (TextView) findViewById(R.id.TextView02);
        startLongitudeField = (TextView) findViewById(R.id.TextView04);
        endLatitudeField = (TextView) findViewById(R.id.textView06);
        endLongitudeField = (TextView) findViewById(R.id.textView08);



        //Button
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v) {

                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                Location location = locationManager.getLastKnownLocation(provider);

                if (startLat != 0) {
                    onLocationChanged(location);
                } else {
                    onLocationChanged(location);
                    Button button = (Button) findViewById(R.id.button1);
                    button.setText("Get me home");
                }
            }
        }); 
    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public void onLocationChanged(Location location) {

        if (startLat == 0) {
            startLat = (double) (location.getLatitude());
            startLng = (double) (location.getLongitude());
            startLatitudeField.setText(String.valueOf(startLat));
            startLongitudeField.setText(String.valueOf(startLng));
        } 
        else {

            endLat = (double) (location.getLatitude());
            endLng = (double) (location.getLongitude());
            endLatitudeField.setText(String.valueOf(endLat));
            endLongitudeField.setText(String.valueOf(endLng));
        }

    }
    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onProviderEnabled(String provider) {


    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {


    }


}

Following is my Error log:

    02-24 12:57:55.580: I/Process(4655): Sending signal. PID: 4655 SIG: 9
02-24 12:58:09.664: D/AndroidRuntime(4772): Shutting down VM
02-24 12:58:09.664: W/dalvikvm(4772): threadid=1: thread exiting with uncaught exception (group=0x41544ba8)
02-24 12:58:09.664: E/AndroidRuntime(4772): FATAL EXCEPTION: main
02-24 12:58:09.664: E/AndroidRuntime(4772): Process: com.example.getmehome, PID: 4772
02-24 12:58:09.664: E/AndroidRuntime(4772): java.lang.IllegalArgumentException: invalid listener: null
02-24 12:58:09.664: E/AndroidRuntime(4772):     at android.location.LocationManager.checkListener(LocationManager.java:1635)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:450)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at com.example.getmehome.MainActivity$1.onClick(MainActivity.java:57)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at android.view.View.performClick(View.java:4438)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at android.view.View$PerformClick.run(View.java:18422)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at android.os.Handler.handleCallback(Handler.java:733)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at android.os.Looper.loop(Looper.java:136)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at java.lang.reflect.Method.invokeNative(Native Method)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at java.lang.reflect.Method.invoke(Method.java:515)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-24 12:58:09.664: E/AndroidRuntime(4772):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 130

Answers (2)

Lucifer
Lucifer

Reputation: 29642

You forgot to initialize provider & locationListener variable that's why it is showing IllegalArgumentException: invalid listener: null

I have edited your code and Now it is working in my device,

public class MainActivity extends Activity 
{
    private TextView startLatitudeField;
    private TextView startLongitudeField;
    private TextView endLatitudeField;
    private TextView endLongitudeField;
    private LocationManager locationManager;
    private LocationListener locationListener;
    private String provider;
    double startLat = 0;
    double startLng = 0;
    double endLat = 0;
    double endLng = 0;

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

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //Setting TextViews for the coordinates
        startLatitudeField = (TextView) findViewById(R.id.txtView1);
        startLongitudeField = (TextView) findViewById(R.id.txtView2);
        endLatitudeField = (TextView) findViewById(R.id.txtView3);
        endLongitudeField = (TextView) findViewById(R.id.txtView4);

        //Button
        Button button = (Button) findViewById(R.id.cmdButton);
        button.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v) {

                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                locationListener = new MyLocationListener ( ); 
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                if (startLat != 0) 
                {
                    locationListener.onLocationChanged(location);
                } else 
                {
                    locationListener.onLocationChanged(location);
                    Button button = (Button) findViewById(R.id.cmdButton);
                    button.setText("Get me home");
                }
            }
        }); 
    };


    private class MyLocationListener implements LocationListener
    {
        @Override
        public void onLocationChanged(Location location) 
        {
            // TODO Auto-generated method stub
            if (startLat == 0) {
                startLat = (double) (location.getLatitude());
                startLng = (double) (location.getLongitude());
                startLatitudeField.setText(String.valueOf(startLat));
                startLongitudeField.setText(String.valueOf(startLng));
            } 
            else {

                endLat = (double) (location.getLatitude());
                endLng = (double) (location.getLongitude());
                endLatitudeField.setText(String.valueOf(endLat));
                endLongitudeField.setText(String.valueOf(endLng));
            }
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }
    }
}

Upvotes: 1

M D
M D

Reputation: 47817

Your Activity must implements LocationListener and implements all the methods.

Update: you should add Provider into Location like:

 Criteria criteria = new Criteria();
 criteria.setAccuracy(Criteria.ACCURACY_FINE);
 Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));

Here you have to used GPS_PROVIDER so that set Accuracy

criteria.setAccuracy(Criteria.ACCURACY_FINE);

And if you used NETWORK_PROVIDER then set Accuracy

criteria.setAccuracy(Criteria.ACCURACY_COARSE);

For more information go to:http://androidexample.com/GPS_Basic_-_Android_Example/index.php?view=article_discription&aid=68&aaid=93

Upvotes: 1

Related Questions