地图活性。java在高于6.0的安卓版本上不起作用
我正在开发地图活动。我的代码在安卓4.4上正常工作,但在另一台6.0设备上不工作
甚至连我在onCreate()下写的吐司都不做。也就是说,地图活动。java不工作。我不知道为什么。下面是我的java、manifest和gradle
谢谢
mapsActivity。java
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPrimary)));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; }
Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show(); //toast and the rest of the code doesn't work on 6.0 and high.
database = FirebaseDatabase.getInstance();
ref = database.getReference("ref");
我的清单。xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission 安卓:name="安卓.permission.ACCESS_FINE_LOCATION" />
<uses-permission 安卓:name="安卓.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission 安卓:name="安卓.permission.INTERNET"></uses-permission>
<application
安卓:allowBackup="true"
安卓:icon="@mipmap/ic_launcher"
安卓:label="@string/app_name"
安卓:roundIcon="@mipmap/ic_launcher_round"
安卓:supportsRtl="true">
<meta-data
安卓:name="com.google.安卓.geo.API_KEY"
安卓:value="@string/google_maps_key" />
<activity
安卓:name=".MapsActivity"
安卓:label="@string/title_activity_maps">
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我的格拉德尔。构建
apply plugin: 'com.安卓.application'
apply plugin: 'com.google.gms.google-services'
安卓 {
compileSdkVersion 27
defaultConfig {
applicationId "com.安卓.app"
minSdkVersion 17
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "安卓.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-安卓.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.安卓.support:appcompat-v7:27.1.1'
implementation 'com.google.安卓.gms:play-services-maps:16.1.0'
testImplementation 'junit:junit:4.12'
安卓TestImplementation 'com.安卓.support.test:runner:1.0.2'
安卓TestImplementation 'com.安卓.support.test.espresso:espresso- core:3.0.2'
implementation 'com.安卓.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-database:16.0.1'
}
# 1 楼答案
它不会运行,因为在android 6.0棉花糖中,您需要请求运行时权限以获得危险权限,您可以查看以下https://developer.android.com/guide/topics/permissions/overview 所以它确实在kitkat上工作,因为已经给出了权限,并且在运行6.0的设备中执行以下代码,从而停止代码执行
你可以这样请求许可:
然后像这样处理结果:
# 2 楼答案
尝试在第一个活动的onCreate方法中添加此代码,如果仍然存在任何错误,请随时与我联系。这将检查位置许可,并在未被授予的情况下请求
自Android M(API 23)以来,每个Android应用程序都需要运行时权限