android - 我在 kotlin 中使用 xml <include/> 标签时崩溃

标签 android xml android-studio kotlin

我在 kotlin 中编码,并在我的布局中使用 from 。 xml,当我调用 bottomsheet_map 布局的子项时,我的应用程序崩溃了。

我的父布局:

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
     ...
    <include layout="@layout/bottomsheet_map" />

    ...

</androidx.constraintlayout.widget.ConstraintLayout>

和 bottomsheet_map.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"
    android:id="@+id/bottomsheet_map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg_app"
    android:elevation="@dimen/medium"
    android:fadingEdgeLength="@dimen/xLarge"
    android:orientation="vertical"
    app:behavior_peekHeight="50dp"
    app:layout_behavior="com.idmpGroup.rtour.utilities.UI.AnchorSheetBehavior">

    <RelativeLayout
        android:id="@+id/tap_action_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/white"
        android:elevation="@dimen/xSmall"
        android:fadingEdgeLength="@dimen/standard"
        android:gravity="center"
        android:paddingStart="@dimen/standard"
        android:paddingTop="@dimen/medium"
        android:paddingEnd="@dimen/standard"
        android:paddingBottom="@dimen/medium">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <View
                android:layout_width="@dimen/xLarge"
                android:layout_height="@dimen/xSmall"
                android:layout_gravity="center"
                android:background="@drawable/dr_btn_radius_solid2" />


            <TextView
                android:id="@+id/bottom_sheet_title"
                style="@style/tvToolbar_style"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/medium"
                android:gravity="center"
                android:text="@string/show_list"
                android:textColor="#979797" />
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

当我想在 onCreateView 中将 OnClickListener 设置为 tap_action_layout 时我崩溃了

 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        val view = inflater.inflate(R.layout.layout, container, false)

        tap_action_layout.setOnClickListener { v: View? ->
          //....
        }
        return view
    }

注意:我不会使用 findViewByID。是否有更好的解决方案来调用 tap_action_layout 而不使用 findViewByID()????

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at com.idmpGroup.rtour.fragments.BAS.NearlyFrag.onCreateView(NearlyFrag.kt:258)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
    at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
    at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
    at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
    at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
    at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
    at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
    at android.os.Handler.handleCallback(Handler.java:907)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7625)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)

最佳答案

onViewCreated 而不是 onCreateView 中试试这个

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        tap_action_layout.setOnClickListener { v: View? ->
          //....
        }
    }

关于android - 我在 kotlin 中使用 xml <include/> 标签时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57990167/

相关文章:

xml - 将节点列表分成两半

android - 如何将我的 Android 手机屏幕投影到我的电脑显示器上?

javascript - Android - 网址加载后更改正文的背景颜色

Android 将下载的数据保存到 SD

android - 某些设备上的 CameraX 黑屏预览

php - 如何给出 php-xml 总计数 ID

xml - SSIS:从 XML 文件到多个表

android - 单击按钮时,应用程序在 android studio 中崩溃。显示应用程序名称已停止工作

android - 每个模块中的方法数

java - 如何在回收器 View 中实现粘性行