2016-10-22 12 views
0

私は現在、アプリケーションが現在の場所を取得し、フラグメント内で起こっているすべてのテキストビューに表示するプロジェクトを行っています。何らかのエラーが出ることはありませんが、何らかの理由で動作しません。Android内のLocationListenerを使用して現在の位置を取得する

私のコードの一部です。 マニフェストとインポートのすべてのアクセス許可はすべて正常です..

ボタンをクリックすると、私のアプリケーションでログカットに何も表示されません。

注:すべてのコードがフラグメント内にある

は、誰かが私にヒントまたは2感謝を与えることができることを望みます!

import android.content.pm.PackageManager; 
import android.location.Location; 
import android.location.LocationManager; 
import android.location.LocationListener; 
private LocationManager locationManager; 
private LocationListener listener; 

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

    // Inflate the layout for this fragment 
    View myinflate = inflater.inflate(R.layout.fragment_home, container, false); 
    final TextView cords = (TextView) myinflate.findViewById(R.id.welcome); 
    final Button butt = (Button) myinflate.findViewById(R.id.button1); 

    locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE); 
    listener = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 
      cords.setText(location.getLatitude() +" "+location.getLongitude()); 
     } 

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

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 
    }; 

    butt.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

       return; 
      }else { 
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener); 
      } 
     } 
    }); 

    return myinflate; 
} 

答えて

0

これは、Android Mバージョンで発生以降

このブロック:

if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

       return; 

は、アプリケーションがアクセス権を持っていない場合は何もしないようにボタンのイベントハンドラを指示します。 >アプリケーション - - >あなたのアプリ - >権限 - >場所の権限を付与

+0

おかげエミュレータから

if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions(LocationSurface.PERMISSIONS_LOCATION, AppConstants.REQUEST_CODE_PERMISSIONS); } }else{....} 

または、設定:

ソリューションは、のいずれかの断片からの許可を要求しています返信のために! – Duckbenok

+0

LocationSurfaceとAppConstantsの2つの変数を解決できませんか? – Duckbenok

+0

LocationSurface&AppConstantsは私のアプリ のpublic static final String []型PERMISSIONS_LOCATION = { Manifest.permission.ACCESS_FINE_LOCATION、 Manifest.permission.ACCESS_COARSE_LOCATION}でカスタム変数です。 パブリックstatic final int REQUEST_CODE_PERMISSIONS = 2; 詳細については、 https://developer.android.com/training/permissions/requesting.htmlを参照してください。 – ahmedmsvb

関連する問題