在Kotlin中使用xml <include />标记时出现崩溃

时间:2019-09-18 10:04:49

标签: android xml android-studio kotlin

我使用 kotlin 进行编码,并在我的 布局中使用 中的代码。 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。是否有更好的解决方案在没有findViewByID()的情况下调用 tap_action_layout ????

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)

6 个答案:

答案 0 :(得分:3)

class InputField { constructor(props) { super(props) } state = { userInput: '' } } onClick = () => { this.setState({ userInput: 'Test' }) } render() { return ( <input value={this.state.userInput} name="sampleInput" /> <button onClick={this.onClick}> Click me </button> ) } 而不是onViewCreated中尝试

onCreateView

答案 1 :(得分:1)

您无法访问代码中直接包含的元素。您需要在包含其他布局的父布局上使用 findviewbyid

例如:

<include layout="@layout/bottomsheet_map" android:id="@+id/include_layout" />

 val tap_action_layout = include_layout.findViewById(R.id.tap_action_layout)
 tap_action_layout.setOnClickListener { v: View? ->
          //....
 }

答案 2 :(得分:1)

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

<include android:id="@+id/my_layout" 
    layout="@layout/bottomsheet_map" />

设置ID之后,您需要先从包含布局标签调用ID,以使您的 tap_action_layout 不可为空,并且应从片段的onViewCreated方法中调用它:(请注意,仅当您使用 kotlinx.android.synthetic 绑定视图时,此方法才有效)

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)

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

答案 3 :(得分:1)

在xml布局中需要将android:id设置为:

<include android:id="@+id/bottomsheet_map_layout" 
    layout="@layout/bottomsheet_map" />

然后调用您的视图以在onViewCreated中进行编码

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    my_layout.top_action_layout.setOnClickListener {
        // do whatever you wanna do here..
    }
}

答案 4 :(得分:0)

我想tap_action_layout需要执行一个视图

尝试一下

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

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

这应该工作完美。

答案 5 :(得分:-1)

我认为您必须设置约束以包含如下所示的标记。

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"