0

I'm making an app which contains 2 Fragments at the moment, one has EditText and a button, the other one has TextView. I want to send my text to another Fragment but I can't succeed doing so. I have tried other threads regarding this but I still couldn't solve my problem, so any help would be appreciated!

This is my first fragment function:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle1?): View? {
        val v= inflater.inflate(R.layout.fragment_start, container, false)



        playButton = v.findViewById(R.id.playButton)
       firstPlayerInput = v.findViewById(R.id.firstPlayerInput)
       secondPlayerInput = v.findViewById(R.id.secondPlayerInput)
        playButton.setOnClickListener {
            nickname = firstPlayerInput.text.toString()
            nickname2 = secondPlayerInput.text.toString()
            Toast.makeText(activity, "First Player: $nickname Second Player: $nickname2", Toast.LENGTH_SHORT).show()
            val nick = firstPlayerInput.text.toString()
            val fr = Fragment()
            val fm = fragmentManager
            val ft = fm!!.beginTransaction()
            val args = Bundle1()
            args.putString("nick", nick)
            fr.arguments = args
            ft.replace(R.id.firstPlayerInput, fr)
            ft.commit()
        }

       playButton.setOnClickListener(
           Navigation.createNavigateOnClickListener(R.id.action_startFragment_to_fragment_one)
       )

and some code from the other one:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val v=  inflater.inflate(R.layout.fragment_one, container, false)

        val strtext = arguments!!.getString("nick")

        Toast.makeText(activity, strtext, Toast.LENGTH_LONG).show()

and the XML file which stores EditText:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context=".StartFragment">

    <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/firstPlayerInput" android:layout_alignParentBottom="true" android:layout_marginBottom="451dp"
            android:layout_alignParentEnd="true" android:layout_marginEnd="98dp"
            android:hint="@string/enter_your_name"/>
    <Button
            android:layout_width="159dp"
            android:layout_height="60dp"
            android:id="@+id/playButton"
            android:layout_marginTop="373dp"
            android:layout_alignParentTop="true" android:layout_alignParentEnd="true" android:layout_marginEnd="121dp"
            android:layout_alignParentStart="true" android:layout_marginStart="130dp"
            android:textColor="#FFFFFF"
            android:textSize="20sp"
            android:text="@string/playgame"
            android:background="@drawable/buttonshape"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="5"
    />
    <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/secondPlayerInput" android:layout_alignParentBottom="true" android:layout_marginBottom="381dp"
            android:layout_alignParentEnd="true" android:layout_marginEnd="97dp"
            android:hint="@string/enter_partner_name"/>


</RelativeLayout>

I get a kotlin.KotlinNullPointerException. I need help with a Kotlin CODE, so please do not post threads with JAVA solutions!!!

Karl
  • 111
  • 8

0 Answers0