首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在kotlin中使用xml <include/>标记时会崩溃

在kotlin中使用xml <include/>标记时会崩溃
EN

Stack Overflow用户
提问于 2019-09-18 10:04:49
回答 5查看 381关注 0票数 1

我正在用kotlin编写代码,并在我的layout.xml中使用,当我调用布局的一个子程序时,我的应用程序崩溃了。

我父母的布局:

代码语言:javascript
复制
 <?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:

代码语言:javascript
复制
<?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>

当我想将OnClickListener设置为tap_action_layout in onCreateView时,我会崩溃

代码语言:javascript
复制
 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
    }

tap_action_layout备注:I不会使用findViewByID .is --更好的解决方案--调用没有findViewByID()?的

代码语言:javascript
复制
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)
EN

Stack Overflow用户

发布于 2019-09-18 10:25:54

您必须首先在xml布局中将android:id设置为include标记:

代码语言:javascript
复制
<include android:id="@+id/my_layout" 
    layout="@layout/bottomsheet_map" />

在您设置id之后,您需要首先调用id from include布局标记,以获得不可空的tap_action_layout,并且您应该从片段中的onViewCreated方法调用它:(请注意,只有在使用kotlinx.android.synthetic处理绑定视图时才能这样做)

代码语言:javascript
复制
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    my_layout.top_action_layout.setOnClickListener {
        // do whatever you wanna do here..
    }
}

否则,如果您不使用kotlin人工合成,您需要首先调用findViewById(R.id.top_action_layout)

代码语言:javascript
复制
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    my_layout.findViewById(R.id.top_action_layout).setOnClickListener {
        // do whatever you wanna do here..
    }
}
票数 1
EN
查看全部 5 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57990167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档