1
votes

J'ai un crash lors de l'utilisation de la balise xml dans kotlin

Je code dans kotlin et j'utilise from dans ma mise en page . xml et lorsque j'appelle un enfant de la mise en page bottomsheet_map , mon application plante.

ma mise en page parent:

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)

et bottomsheet_map.xml:

 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
    }

lorsque je veux définir OnClickListener sur tap_action_layout dans onCreateView, j'ai un crash

<?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>


3 commentaires

postez votre erreur plZ


pouvez-vous s'il vous plaît ajouter un journal des erreurs?


si vous utilisez findViewByID pour appeler l'enfant, il n'y a pas de problème ... je n'utiliserais pas findViewByID car mon nombre d'enfants est très élevé


5 Réponses :


3
votes

essayez ceci dans onViewCreated au lieu de onCreateView

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


0 commentaires

1
votes

Vous ne pouvez pas accéder directement aux éléments inclus dans votre code. Vous devez utiliser findviewbyid sur la mise en page parent dans laquelle vous avez inclus une autre mise en page.

Par exemple:

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


0 commentaires

1
votes

Vous devez d'abord définir android: id sur inclure la balise dans la mise en page xml:

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

Après avoir défini l'identifiant, vous devez call id from include layout tag first to get your tap_action_layout not nullable and you should call it from onViewCreated method in your fragment: (notez que cela ne fonctionne que si vous utilisez kotlinx.android.synthetic pour lier les vues)

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

Sinon, si vous n'utilisez pas de kotlin synthétique, vous devrez appeler findViewById (R .id.top_action_layout) d'abord:

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


0 commentaires

0
votes

Je suppose que tap_action_layout a besoin d'une vue pour être exécuté

Essayez ceci

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

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

Cela devrait fonctionner parfaitement.

p>


0 commentaires

1
votes

dans la mise en page xml, vous devez définir android: id sur:

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

puis appeler vos vues à coder dans onViewCreated

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


0 commentaires