首页 > 解决方案 > avc: 拒绝 { 读取 } 片段 Android

问题描述

我有一个用 XML 创建的模式对话框。然后,我在kotlin中展示它(它不是一个真正的对话框视图,而不仅仅是一个信息弹出窗口)。

请参阅 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AlertsDialogRemi"
    android:id="@+id/alertLayoutRoot"
    android:orientation="vertical"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="UselessParent"
        >

        <RelativeLayout
            android:id="@+id/layoutTopAlert"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="33dp"
            android:gravity="center"
            >

            <LinearLayout
                android:id="@+id/layoutAlert"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@color/orange"
                >

                <TextView
                    android:id="@+id/alerte_title"
                    style="@style/titleActu"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="66dp"
                    android:text="@string/alertes"
                    android:textColor="@color/colorWhite" />


                <ViewFlipper
                    android:id="@+id/viewFlipperAlert"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/orange">
                </ViewFlipper>

                <LinearLayout
                    android:id="@+id/dotNavAlert"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="horizontal">

                </LinearLayout>

            </LinearLayout>
            <Button
                android:id="@+id/close_modal_alerte"
                style="@style/Base.Widget.AppCompat.Button.Borderless"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:background="@android:color/transparent"
                android:text="Fermer"
                android:layout_below="@id/layoutAlert"/>
        </RelativeLayout>
        <ImageView
            android:id="@+id/alert_logo"
            android:layout_width="66dp"
            android:layout_height="66dp"
            android:layout_centerHorizontal="true"
            android:src="@drawable/alertes" />


    </RelativeLayout>


</LinearLayout>

此函数显示 MainActivity.kt 中的模式(我正在使用片段和导航抽屉):

lateinit var mydialog : Dialog
lateinit var txt : TextView

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        var fragment: Fragment? = null

        when (item.itemId) {
            R.id.action_alert -> {
                showDialog()
                return true
            }
            else -> return super.onOptionsItemSelected(item)
        }
    }

    fun showDialog(){
        mydialog = Dialog(this, R.style.DialogCustomTheme)
        mydialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        mydialog.setContentView(R.layout.alerts_dialog_remi)

        txt = mydialog.findViewById(R.id.close_modal_alerte)
        txt.isEnabled = true
        txt.setOnClickListener{
            mydialog.cancel()
        }
        mydialog.show()
    }

在 AlertsDialogRemi.xml 中,我有这个:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val alerts = arrayOf("AlertsDialogRemi 1", "AlertsDialogRemi 2", "AlertsDialogRemi 3")
    for(alert in alerts){
        Log.i(TAG, "Alert : $alert")
    }

当我尝试访问 AlertDialogRemi.kt 中的警报变量时,我的日志中出现此错误:

W/RenderThread: type=1400 audit(0.0:6789172): avc: denied { read } for name="perf_ioctl" dev="proc" ino=4026533700 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u :object_r:proc:s0 tclass=文件许可=0

这很奇怪,因为我在另一个片段(Accueil.kt)中做了同样的事情,但我一点问题都没有。

编辑:显然,这是由于对话框,如果我在没有对话框的情况下调用片段,我有我的数据。那么我应该改变什么?

标签: androidandroid-fragmentskotlinfragment

解决方案


您应该mydialog.create()在调用mydialog.show()方法或尝试访问其内部视图之前调用。

fun showDialog(){
    mydialog = Dialog(this, R.style.DialogCustomTheme)
    mydialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    mydialog.setContentView(R.layout.alerts_dialog_remi)

    mydialog.create()

    txt = mydialog.findViewById(R.id.close_modal_alerte)
    txt.isEnabled = true
    txt.setOnClickListener{
        mydialog.cancel()
    }
    mydialog.show()
}

推荐阅读