Android 9(API 28)活动在启动(setContentView)时崩溃

时间:2019-02-08 10:35:39

标签: android kotlin android-9.0-pie

将手机从Android 8更新到9时,问题开始了。 该应用之前运行良好。

我试图通过包含相同的依赖项/配置在我正在处理的其他应用程序中复制此文件,但是我无法复制该行为。即使对于简单的活动和简单的布局,它始终会在setContentView上崩溃。

我做了研究,得出的结论是,INTERACT_ACROSS_USERS_FULL许可不是造成此问题的真正原因。

此崩溃实际上发生在所有应用程序活动中,但TestActivity是最简单的活动。在运行API 28的Samsung S9 +和运行Android 9,API 28的Pixel Emulator上进行了测试。

编辑:当我将android:name =“ ....”属性放入清单定义时,似乎会发生这种情况。由于我确实需要应用程序类,因此我仍在寻找一种解决方案来实现此目的。


活动(简化为发生崩溃所需的最少代码):

package com.marhat.albatros

import android.os.Bundle import androidx.appcompat.app.AppCompatActivity;

class TestActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".TestActivity">

</androidx.coordinatorlayout.widget.CoordinatorLayout>

这是输出:

2019-02-08 11:54:19.088 20298-20298/com.marhat.albatros E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.marhat.albatros, PID: 20298
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.marhat.albatros/com.marhat.albatros.TestActivity}:
     

java.lang.SecurityException:以用户1的身份从用户0调用而没有   权限INTERACT_ACROSS_USERS或INTERACT_ACROSS_USERS_FULL不   允许的。               在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)               在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)               在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)               在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)               在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)               在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1808)               在android.os.Handler.dispatchMessage(Handler.java:106)               在android.os.Looper.loop(Looper.java:193)               在android.app.ActivityThread.main(ActivityThread.java:6669)               在java.lang.reflect.Method.invoke(本机方法)               在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)            由以下原因引起的:java.lang.SecurityException:未经许可INTERACT_ACROSS_USERS或以用户1的身份从用户0调用   不允许INTERACT_ACROSS_USERS_FULL。               在android.os.Parcel.createException(Parcel.java:1942)               在android.os.Parcel.readException(Parcel.java:1910)               在android.os.Parcel.readException(Parcel.java:1860)               在android.view.accessibility.IAccessibilityManager $ Stub $ Proxy.addClient(IAccessibilityManager.java:295)               在android.view.accessibility.AccessibilityManager.tryConnectToServiceLocked(AccessibilityManager.java:1125)               在android.view.accessibility.AccessibilityManager。(AccessibilityManager.java:358)               在android.view.accessibility.AccessibilityManager.getInstance(AccessibilityManager.java:336)               在android.view.View.setFlags(View.java:14008)               在android.view.View。(View.java:5565)               在android.view.ViewGroup。(ViewGroup.java:659)               在android.view.ViewGroup。(ViewGroup.java:655)               在android.view.ViewGroup。(ViewGroup.java:651)               在android.view.ViewGroup。(ViewGroup.java:647)               在android.widget.FrameLayout。(FrameLayout.java:78)               在com.android.internal.policy.DecorView。(DecorView.java:255)               在com.android.internal.policy.PhoneWindow.generateDecor(PhoneWindow.java:2310)               在com.android.internal.policy.PhoneWindow.installDecor(PhoneWindow.java:2662)               在com.android.internal.policy.PhoneWindow.getDecorView(PhoneWindow.java:2071)               在androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:575)               在androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:518)               在androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:466)               在androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)               在com.marhat.albatros.TestActivity.onCreate(TestActivity.kt:9)               在android.app.Activity.performCreate(Activity.java:7136)               在android.app.Activity.performCreate(Activity.java:7127)               在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)               在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)               在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)               在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)               在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)               在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)               在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1808)               在android.os.Handler.dispatchMessage(Handler.java:106)               在android.os.Looper.loop(Looper.java:193)               在android.app.ActivityThread.main(ActivityThread.java:6669)               在java.lang.reflect.Method.invoke(本机方法)               在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)            引起原因:android.os.RemoteException:远程堆栈跟踪:               在com.android.server.accessibility.AccessibilityManagerService $ SecurityPolicy.resolveCallingUserIdEnforcingPermissionsLocked(AccessibilityManagerService.java:3522)               在com.android.server.accessibility.AccessibilityManagerService.addClient(AccessibilityManagerService.java:487)               在android.view.accessibility.IAccessibilityManager $ Stub.onTransact(IAccessibilityManager.java:82)               在android.os.Binder.execTransact(Binder.java:731)


清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.marhat.albatros">

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

    <uses-feature
            android:name="android.hardware.usb.host"
            android:required="true"/>
    <uses-feature android:name="android.hardware.location.gps"/>

    <application
            android:name=".AlbatrossApplication"
            android:allowBackup="true"
            android:icon="@mipmap/ic_icon_main"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_icon_main_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
        <activity
                android:name=".TestActivity"
                android:label="@string/title_activity_test"
                android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity
                android:name=".TasksActivity"
                android:label="@string/title_activity_tasks"
                android:theme="@style/AppTheme.NoActionBar"/>

        <meta-data
                android:name="com.google.android.geo.API_KEY"
                android:value="xxx"/>

        <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>

        <service
                android:name=".UsbService"
                android:enabled="true"/>

        <activity android:name=".FPViewActivity"/>
        <activity
                android:name=".SplashActivity"
                android:theme="@style/AppTheme.Launcher">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity
                android:name=".TaskViewActivity"
                android:theme="@style/AppTheme">
            <intent-filter android:label="@string/app_name">
                <action android:name="android.intent.action.VIEW"/>

                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

                <data android:mimeType="*/*"/>
                <data android:scheme="file"/>
                <data android:scheme="content"/>
                <data android:pathPattern=".*\\.xfd"/>
                <data android:host="*"/>
            </intent-filter>
        </activity>
        <activity
                android:name=".SettingsActivity"
                android:windowSoftInputMode="stateVisible|adjustResize"/>
    </application>

</manifest>

项目gradle文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.3.21'
    repositories {
        google()
        jcenter()
        maven {
            url 'https://maven.fabric.io/public'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.2.0'
        classpath 'io.fabric.tools:gradle:1.25.4'
    } }

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url 'https://maven.google.com/'
        }

        maven {
            url "https://jitpack.io"
        }
    } }

task clean(type: Delete) {
    delete rootProject.buildDir }

应用程序/ gradle文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'io.fabric'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.marhat.albatros"
        minSdkVersion 18
        targetSdkVersion 28
        multiDexEnabled true
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        // vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    dataBinding {
        enabled = true
    }
}

dependencies {
    implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
    implementation 'com.android.support:multidex:1.0.3'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.android.support:appcompat-v7:28'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.vectordrawable:vectordrawable:1.0.1'
    implementation 'com.github.QuadFlask:colorpicker:0.0.13'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'com.android.support:support-core-utils:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.google.firebase:firebase-core:16.0.7'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.8'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1'
    implementation 'com.google.android.gms:play-services-maps:16.1.0'
    implementation 'com.google.maps.android:android-maps-utils:0.5'
    def room_version = "2.1.0-alpha04"
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    testImplementation "androidx.room:room-testing:$room_version"
    def lifecycle_version = "2.0.0"
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version"
    testImplementation "androidx.arch.core:core-testing:$lifecycle_version"
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
    implementation 'com.google.code.gson:gson:2.8.5'

    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'

}

apply plugin: 'com.google.gms.google-services'

2 个答案:

答案 0 :(得分:0)

问题在于应用程序类实现了名为getUserId的方法。 重命名该方法后,所有操作将再次在Android 9上正常工作。

答案 1 :(得分:0)

我终于找到了可行的解决方案。 1)->将解析复制到清单中

<!--Support Android Pie 9-->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" android:protectionLevel="signature" />

2)->查找并替换所有getUserId出现 对于Mac用户:使用Cmd + Shift + R 在MAC上的示例

https://i.stack.imgur.com/eLtWR.png