Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 子片段ConstraintLayout上的NullPointException_Android_Xml_Kotlin - Fatal编程技术网

Android 子片段ConstraintLayout上的NullPointException

Android 子片段ConstraintLayout上的NullPointException,android,xml,kotlin,Android,Xml,Kotlin,我正在尝试更改我的ConstraintLayoutmy code的背景: fragment_user.xml <fragment android:id="@+id/profile" android:name="com.app.ProfileFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".HomeActi

我正在尝试更改我的
ConstraintLayout
my code的背景:

fragment_user.xml

<fragment
    android:id="@+id/profile"
    android:name="com.app.ProfileFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeActivity"
    xmlns:tools="http://schemas.android.com/tools"/>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/colorPopupBackground"
    android:fadeScrollbars="false"
    android:id="@+id/profile_layout">
替换代码

 profile_layout.setBackgroundColor(ContextCompat.getColor(activity!!, R.color.colorPrimaryDark))

请写:

activity?.let{ activity -> profile_layout?.setBackgroundColor(ContextCompat.getColor(activity,R.color.colorPrimaryDark)) 
}

改为从父对象的子对象访问父对象:

val parent = child.parent as ConstraintLayout
        parent.setBackgroundColor(ContextCompat.getColor(activity!!, R.color.colorTransparent))

现在可以使用了。

您是否从布局中分配了它
profile\u layout=findviewbyd(R.id.profile\u layout)
你能发布你的错误日志吗Logcated logcat@sasikumarYes,我得到了相同的空异常@MetaSnarf,我已经在我的edit中添加了代码,其中showProfile是从中调用的。onViewCreated()?你用的是kotlin合成纤维吗?。将showProfile移动到onViewCreated。需要API 23,我正在将API 21作为最小SDK运行。转到应用程序构建。渐变将minSDK版本更改为23我不想更改minSDK@Sasikumars,直到不更改背景色
activity?.let{
profile_layout.setBackgroundColor(it.getColor(R.color.colorPrimaryDark))
}
activity?.let{ activity -> profile_layout?.setBackgroundColor(ContextCompat.getColor(activity,R.color.colorPrimaryDark)) 
}
val parent = child.parent as ConstraintLayout
        parent.setBackgroundColor(ContextCompat.getColor(activity!!, R.color.colorTransparent))