2016-09-02 10 views
0

私はアンドロイドが新しく、私の問題をGoogleで検索したところ、問題に適切な解決策を見つけることができませんでした。Androidアプリケーションが存在し、「残念ながらTestGPSが停止しました」と表示されます

私は、ユーザーの場所を自動的に見つけることができるロケーションベースのAndroidアプリケーションを開発しようとしています。私のコードは、Android Studio Managerでエラーなしでコンパイルが、私はShow Locationボタンをクリックしたときに、私のアプリケーションが存在すると、エラーUnfortunately, TestGPS has stoppedenter 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) 

ここで問題は何ですか?

+0

問題があなたのshowSettingsAlert()関数にあります。 AlertDialog.BuilderのalertDialog = new AlertDialog.Builder(mContext);をAlertDialog.BuilderのalertDialog =新しいAlertDialog.Builder(getApplicationContext());に置き換えてみてください。 – lawstud

+0

またはonCreate set mContext = getApplicationContext() –

+0

GPSTrackerクラスとあなたのアクティビティをマージしないでください。単純な方法はGpsTracker.javaとActivtyを分離し、Activity-onCreate()でGpsTrackerのインスタンスを作成し、コンテキストを渡します。 onClick()はLocation location = gpsTrackerInstance.getLocation()と呼ばれます。 –

答えて

0

問題がGPSTrackermContextnullであるということです、そしてあなたはヌルContextAlertDialogを作成しています。
また、アクティビティのコンストラクタをオーバーライドしないでください。移動 あなたgetLocation();方法onCreate()

+0

'mContext'はプライベートな最終変数です。コンストラクタで定義しました。あなたはこれを解決する方法を説明できますか? –

+0

@LalindaSampath、これは 'GPSTracker'の使用方法に依存します –

+0

私は完全なコードを与えました。 –

0

変数「mContext」は、最終的な定義されており、それがinitialized.Iコードがアンドロイドスタジオでエラーなしでコンパイルとは思わないされていません。 mContextをfinalにする必要はなく、onCreate()メソッドで初期化する必要があります。あなたがmContext finalだけを望むなら、これを見てください:

private final Context mContext = this; 
関連する問題