Android应用程序存在并显示错误“不幸的是,TestGPS已停止”

时间:2016-09-02 07:28:09

标签: java android geolocation android-gps

我是Android的新手,当我搜索我的问题时,我无法找到解决问题的正确方法。

我正在尝试开发一个基于位置的Android应用程序,可以自动查找用户位置。我的代码在android studio manager中没有任何错误编译,但当我点击Show Location按钮时,我的应用程序存在并显示错误Unfortunately, TestGPS has stopped enter image description here

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lsdias.testgps" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".GPSTracker"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

</manifest>

GPSTracker.java

package com.example.lsdias.testgps;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class GPSTracker extends ActionBarActivity implements LocationListener
{
    Button btnShowLocation;
    private final Context mContext;

    // flag for GPS status
    boolean isGPSEnabled = false;

    // flag for network status
    boolean isNetworkEnabled = false;

    boolean canGetLocation = false;

    Location location; // location
    double latitude; // latitude
    double longitude; // longitude

    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

    // Declaring a Location Manager
    protected LocationManager locationManager;

    public GPSTracker()
    {
        mContext = null;
    }

    public GPSTracker(Context mContext)
    {
        this.mContext = mContext;
        getLocation();
    }

    public Location getLocation()
    {
        try
        {
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled)
            {
                // no network provider is enabled
            }
            else
            {
                this.canGetLocation = true;
                // First get location from Network Provider
                if (isNetworkEnabled)
                {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null)
                    {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null)
                        {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled)
                {
                    if (location == null)
                    {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null)
                        {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null)
                            {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return location;
    }

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

        btnShowLocation = (Button) findViewById(R.id.button_Location);

        // show location button click event
        btnShowLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(canGetLocation())
                {
                    double latitude = getLatitude();
                    double longitude = getLongitude();

                    // \n is for new line
                    Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
                }
                else
                {
                    // can't get location
                    // GPS or Network is not enabled
                    // Ask user to enable GPS/network in settings
                    showSettingsAlert();
                }
            }
        });
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onLocationChanged(Location location) {

    }

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

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    /**
     * Function to get latitude
     * */
    public double getLatitude(){
        if(location != null){
            latitude = location.getLatitude();
        }

        // return latitude
        return latitude;
    }

    /**
     * Function to get longitude
     * */
    public double getLongitude(){
        if(location != null){
            longitude = location.getLongitude();
        }

        // return longitude
        return longitude;
    }

    /**
     * Function to check if best network provider
     * @return boolean
     * */
    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    /**
     * Function to show settings alert dialog
     * */
    public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

        // Setting Dialog Title
        alertDialog.setTitle("GPS is settings");

        // Setting Dialog Message
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

        // Setting Icon to Dialog
        //alertDialog.setIcon(R.drawable.delete);

        // On pressing Settings button
        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(intent);
            }
        });

        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }

    /**
     * Stop using GPS listener
     * Calling this function will stop using GPS in your app
     * */
    public void stopUsingGPS(){
        if(locationManager != null){
            locationManager.removeUpdates(GPSTracker.this);
        }
    }
}

logcat的

09-02 12:47:51.902    1935-1935/com.example.lsdias.testgps I/art﹕ Not late-enabling -Xcheck:jni (already on)
09-02 12:47:52.384    1935-1958/com.example.lsdias.testgps D/OpenGLRenderer﹕ Render dirty regions requested: true
09-02 12:47:52.385    1935-1935/com.example.lsdias.testgps D/﹕ HostConnection::get() New Host Connection established 0xa6c40550, tid 1935
09-02 12:47:52.418    1935-1935/com.example.lsdias.testgps D/Atlas﹕ Validating map...
09-02 12:47:52.582    1935-1949/com.example.lsdias.testgps I/art﹕ Background sticky concurrent mark sweep GC freed 4140(288KB) AllocSpace objects, 0(0B) LOS objects, 28% free, 810KB/1135KB, paused 61.733ms total 123.372ms
09-02 12:47:52.607    1935-1958/com.example.lsdias.testgps D/﹕ HostConnection::get() New Host Connection established 0xa6c406b0, tid 1958
09-02 12:47:52.675    1935-1958/com.example.lsdias.testgps I/OpenGLRenderer﹕ Initialized EGL, version 1.4
09-02 12:47:52.697    1935-1958/com.example.lsdias.testgps D/OpenGLRenderer﹕ Enabling debug mode 0
09-02 12:47:52.764    1935-1958/com.example.lsdias.testgps W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-02 12:47:52.764    1935-1958/com.example.lsdias.testgps W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c1ae20, error=EGL_SUCCESS
09-02 12:47:53.478    1935-1958/com.example.lsdias.testgps W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-02 12:47:53.478    1935-1958/com.example.lsdias.testgps W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c1ae20, error=EGL_SUCCESS
09-02 12:47:58.675    1935-1935/com.example.lsdias.testgps D/AndroidRuntime﹕ Shutting down VM
    --------- beginning of crash
09-02 12:47:58.675    1935-1935/com.example.lsdias.testgps E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.lsdias.testgps, PID: 1935
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
            at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:154)
            at android.app.AlertDialog$Builder.<init>(AlertDialog.java:379)
            at com.example.lsdias.testgps.GPSTracker.showSettingsAlert(GPSTracker.java:240)
            at com.example.lsdias.testgps.GPSTracker$1.onClick(GPSTracker.java:155)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

这里有什么问题?

2 个答案:

答案 0 :(得分:0)

问题是mContext中的GPSTrackernull,而您创建的AlertDialog为空Context
此外,您不应该覆盖Activity的构造函数。 将您的getLocation();移至onCreate()方法

答案 1 :(得分:0)

变量'mContext'是最终定义的,它尚未初始化。我不认为代码在android studio中没有任何错误编译。你不需要让mContext成为final,并且应该在onCreate()方法中初始化。如果你只想要mContext final,请看:

{{1}}