2016-09-12 8 views
0

私のアンドロイドアプリケーションでgoogle map v2を使用しようとしています。すべてエミュレータで動作しますが、私がマップ上でsetMyLocationEnabled(true)を使用すると、実際のデバイスでクラッシュします。実際のデバイスでmapcallbackでsetMyLocationEnabledを使用した後にアンドロイドがクラッシュする

このデバイスは、アンドロイドv6を使用しているlg x-camです。ここ

は私のマニフェストです:

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

    <uses-sdk 
     android:minSdkVersion="17" 
     android:targetSdkVersion="24" /> 


    <permission 
     android:name="com.example.latest.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature" /> 

    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true" /> 



    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="com.example.latest.permission.MAPS_RECEIVE" /> 



    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/Theme.AppCompat.Light" 
     > 

     <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="my valid api key is here"/> 

     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 

     <activity 
      android:name=".MainActivity" 
      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> 

</manifest> 

私MainActivity:

package com.example.latest; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 

import android.app.Activity; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback { 

    static final LatLng HAMBURG = new LatLng(53.558, 9.927); 
    static final LatLng KIEL = new LatLng(53.551, 9.993); 
    SupportMapFragment mapFragment; 

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

     mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 

    } 

    @Override 
    public void onMapReady(GoogleMap map) { 
     map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
     map.setMyLocationEnabled(true); 
     map.getUiSettings().setZoomControlsEnabled(true); 
     map.getUiSettings().setCompassEnabled(true); 
     map.getUiSettings().setZoomGesturesEnabled(true); 
     map.getUiSettings().setMyLocationButtonEnabled(true); 
     map.getUiSettings().setMapToolbarEnabled(true); 

    } 

} 

私activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 

    <fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     class="com.google.android.gms.maps.SupportMapFragment" /> 

</RelativeLayout> 

どのように私はこの問題を解決することができますか?

+1

*スタックトレース*ポストいっぱい。 'SDK'バージョン' 6.0'で作業している場合、 'Run Time permission'を提供する必要があります。 –

+0

正しいアクセス権を要求していることを確認してください:https://developer.android.com/training/permissions/requesting.html –

+0

@jaydroiderこの問題のために私の活動で使用するコードは何ですか?あなたはもっと具体的になりますか?ありがとうございます。 –

答えて

2

Android Mを実行していますか?その場合は、マニフェストでパーミッションを宣言するだけでは不十分であるためです。一部のアクセス許可については、実行時に明示的にユーザーに問い合わせる必要があります。

実行時アクセス許可の場合はLinkをチェックしてください。

コードスニペット

これをグローバルに定義します。

private static final int PERMISSION_REQUEST_CODE = 1; 

そして、この意志のonCreateに行きます。

if (ContextCompat.checkSelfPermission(Maps_Activity.this, 
     Manifest.permission.ACCESS_FINE_LOCATION) 
     != PackageManager.PERMISSION_GRANTED) { 

    // Should we show an explanation? 
    if (ActivityCompat.shouldShowRequestPermissionRationale(Maps_Activity.this, 
      Manifest.permission.ACCESS_FINE_LOCATION)) { 

     // Show an expanation to the user *asynchronously* -- don't block 
     // this thread waiting for the user's response! After the user 
     // sees the explanation, try again to request the permission. 

    } else { 

     // No explanation needed, we can request the permission. 

     ActivityCompat.requestPermissions(Maps_Activity.this, 
       new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
       PERMISSION_REQUEST_CODE); 


    } 
} 

そしてoverrride onRequestPermissionsResult.

@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case PERMISSION_REQUEST_CODE: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! 

      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
      } 
      return; 
     } 

     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
} 
+0

ACCESS_FINE_LOCATIONは解決できないか、フィールドではありません。このエラーに対処する方法? –

+0

このようにしてみてください 'android.Manifest.permission.ACCESS_FINE_LOCATION'。 –

+0

それは魅力のように機能します。あなたに感謝します。 –

関連する問題