2016-05-02 9 views
0

私のLocation Listenerに問題があると思うが、それを特定することはできない。 GPSを取得するためのコードを削除すると、アプリケーションは正常に動作しています。私はフラグメント内からGPS座標を取得しようとしています。エラーは発生していないが、起動時にアプリケーションがクラッシュする

public class RescueFragment extends Fragment{ 

    public RescueFragment(){} 
    private static final long mindisch=1; 
    private static final long mintim=1000; 
    protected LocationManager locationManager; 
    protected Double latitude,longitude; 

    TextView alert_display; 
    Button btn; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.fragment_rescue, container, false); 
     alert_display = (TextView)rootView.findViewById(R.id.alert_display);  
     btn = (Button) rootView.findViewById(R.id.button_rescue_me); 

     locationManager =(LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,mindisch,mintim, new MyLocationListener() ); 

     btn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 


       alert_display.setText("Alert Sent to User"); 
       showCurrentLocation(); 
      } 
     }); 

     return rootView; 
    } 

    protected void showCurrentLocation() 
    { 
     Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if(location!=null) 
     { 
      latitude=location.getLatitude(); 
      longitude=location.getLongitude(); 
      Toast.makeText(getActivity(), "Alert sent. Location: "+ latitude+" "+ longitude, Toast.LENGTH_LONG).show(); 
     } 
    } 

    private class MyLocationListener implements LocationListener{ 

     @Override 
     public void onLocationChanged(Location location) { 
      latitude=location.getLatitude(); 
      longitude=location.getLongitude(); 
      Toast.makeText(getActivity(), "Alert sent. Location: "+ latitude+" "+ longitude, Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      Toast.makeText(getActivity(), "Provider Status Changed: ", Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void onProviderEnabled(String provider) { 
      Toast.makeText(getActivity(), "GPS ON", Toast.LENGTH_LONG).show();   
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      Toast.makeText(getActivity(), "GPS OFF:", Toast.LENGTH_LONG).show();    
     } 
    } 
} 
+3

エラーログを親切に投稿してください。 –

+0

あなたの 'LogCat'を投稿してください。 –

+0

マニフェストファイルの場所を取得する権限を追加しましたか? – himanshu1496

答えて

1

あなたのマニフェストにこのPermissionsを追加します。 Android Versions 6.0については

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

あなたは、ユーザーのRuntime Permissionsを与えることを可能にする必要があります。

if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == 
     PackageManager.PERMISSION_GRANTED && 
     ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == 
     PackageManager.PERMISSION_GRANTED) { 
    googleMap.setMyLocationEnabled(true); 
    googleMap.getUiSettings().setMyLocationButtonEnabled(true); 
} else { 
    Toast.makeText(this, R.string.error_permission_map, Toast.LENGTH_LONG).show(); 
} 
+0

<?xml version = "1.0" encoding = " &gt; <マニフェストxmlns:アンドロイド= "http://schemas.android.com/apk/res/android" パッケージ= "info.androidhive.slidingmenu" アンドロイド:versionCode = "1" アンドロイド:versionName = "1.0"> <使用-SDK アンドロイド:minSdkVersionが= "14" アンドロイド:targetSdkVersion = "23" /> <使用許可アンドロイド:名= "QRアプリケーション" /> android:allowBackup = "true" – naveen

+0

'SDK'バージョン6.0をコンパイルしていますか?はいの場合は、ユーザーがあなたのアプリで 'Runtime Permission'を持つことを許可する必要があります。このリンクを参照してくださいhttp://stackoverflow.com/questions/32083913/android-gps-requires-access-fine-location-error-even-though-my-manifest-file –

+0

私はそれをアプリケーションタグの前に追加しました。私はそれを位置を変える? – naveen

関連する問題