2017-05-15 2 views
0

이전 업데이트가 새 업데이트와 함께 있지만 이전 버전과 호환됩니다. 이제 요청 권한이 필요합니다. 허가를 보여줄 수 있어요. 그러나 문제는 "TestNavigationDrawer가 중지되었습니다."또한 시작 요청에 PERMISSION REQUESTS와 함께 나타납니다. 나는 요청 권한을 어디에 두어야할지 잘 모르겠다. 이걸 도와주세요. 미리 감사드립니다. 또한이 명확하게 스크린 샷을 제공합니다 ("TestNavigationDrawer가 중지"또한 권한 요청과 함께 시작에 나타납니다.)시작시 Android 요청 권한

Click here to see the screenshot

이 내 코드입니다. 심지어 오류를 피하기 위해 onCreate의 최상위에 권한 요청을 넣습니다.

protected void onCreate(Bundle savedInstanceState) { 
    if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == 
PackageManager.PERMISSION_GRANTED){ 
     System.out.println("Permission Granted"); 
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, this); 
    } else{ 
     String[] permissionRequested={Manifest.permission.ACCESS_FINE_LOCATION}; 
     requestPermissions(permissionRequested, ACCESS_FINE_LOCATION_REQUEST_CODE); 
    } 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    /* HomeFragment fragment = new HomeFragment(); 
    android.support.v4.app.FragmentTransaction fragmentTransaction = 
      getSupportFragmentManager().beginTransaction(); 
    fragmentTransaction.replace(R.id.fragment_container, fragment); 
    fragmentTransaction.commit();*/ 

    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 



    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 

} 
@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    if (requestCode == ACCESS_FINE_LOCATION_REQUEST_CODE) { 
     if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

     } else { 
      Toast.makeText(this, "PERMISSION NOT GRANTED", Toast.LENGTH_LONG).show(); 
     } 
    } 
} 
+0

avigateView.setNavigationItemSelectedListener (this); –

+0

Hello @DivyeshPatel. 우선, 내 게시물에 응답 해 주셔서 감사합니다. 방금 제안한 솔루션을 시도했지만 여전히 작동하지 않았습니다. 또한 권한을 표시하기 전에 앱이 먼저 중지되었습니다. 그것은 내 오류 로그에서 여전히 같은 오류입니다. 여기있어. "gps"위치 공급자는 ACCESS_FINE_LOCATION 권한이 필요합니다. – IWantToBeAnAnonymous

답변

0

사실, 당신은 별도의 기능에 모든 코드를 넣고 onRequestPermissionResult에서 그 함수를 호출 .. 사용 권한을 부여 할 때 등등 권한 요청 아래의 모든 물건 (을 locationManager)를 호출 할 필요가 ...

+0

이것이 작동 중입니다. 지금 코드를 업데이트했습니다. 아이디어를 가져 주셔서 대단히 감사합니다. 비록 시간이 있다면, 당신은 어떤 개발자를 위해 그것을하는 방법에 대한보다 구체적인 지침을 추가 할 수 있습니다. 그러나 나는 당신이 나를 어떻게하려고하는지 이해할 수있었습니다. 고마워, 어머! – IWantToBeAnAnonymous

+0

글쎄, 행운과 행복 코딩 .. –

0

로 코드를 바꿉니다 아래

protected void onCreate(Bundle savedInstanceState) { 
if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == 
PackageManager.PERMISSION_GRANTED){ 
    System.out.println("Permission Granted"); 


LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, this); 

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
     this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
drawer.setDrawerListener(toggle); 
toggle.syncState(); 

navigationView = (NavigationView) findViewById(R.id.nav_view); 
navigationView.setNavigationItemSelectedListener(this); 
} else{ 
    String[] permissionRequested={Manifest.permission.ACCESS_FINE_LOCATION}; 
    requestPermissions(permissionRequested, ACCESS_FINE_LOCATION_REQUEST_CODE); 
} 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 

}

때문에 당신은면 난을 위치 관리자의 코드를 유지하는 경우 f 권한이 주어지지 않고 여전히 충돌하는 위치에 액세스하고 있다면 권한을 의미하는 조건문. 코드가 통과해야하는 권한이 부여 된 후 사용 위치 if 문 도움이 되길 바랍니다

+0

안녕하세요, 압둘. 답변 해 주셔서 감사합니다. 나는 Umer의 도움으로 그것을 해결했다. 귀하의 접근 방식도 그와 동일하지만 if 문 안에 서랍 레이아웃을 추가해서는 안됩니다. 우리가 그렇게한다면 또 다른 오류를 던지고있다. 그래도 고마워 :) – IWantToBeAnAnonymous