单击按钮时,我希望将Fragment 1布局替换为Fragment 2布局,但抛出该异常:java.lang.NullPointerException

问题描述 投票:1回答:2

第一个片段的代码

class MainFragment : Fragment() {

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val mainInflater = inflater.inflate(R.layout.fragment_main, container, false)
    return mainInflater
}

fun thisdata():String{

    return "Hello from MainFragment"
}

}

第一片段XML

<?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=".MainFragment">

    <EditText
        android:id="@+id/etSavedData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:hint="Enter the Text"/>

    <Button
        android:id="@+id/btnSaveData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="120dp"
        android:text="Save"/>

</RelativeLayout>

主要活动的代码

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    //Set replace Main Activity content with the Fragment1 content
    val mainFragment = MainFragment()

    supportFragmentManager.beginTransaction().add(R.id.contain_fragment, mainFragment).commit()
    val thedata = mainFragment.thisdata()
    Log.e("Main Frag to Activity", thedata)

    btnSaveData.setOnClickListener { 

        val secondFragment = SecondFragment()
        supportFragmentManager.beginTransaction().add(R.id.contain_fragment, secondFragment).commit()
    }

}

}

主要活动XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/contain_fragment"
    tools:context=".MainActivity">


</RelativeLayout>

第二片段的代码

class SecondFragment : Fragment() {

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val secondInflater = inflater.inflate(R.layout.fragment_second, container, false)
    return secondInflater
}

}

第二片段XML

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    android:background="@android:color/holo_blue_dark"
    tools:context=".SecondFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/tvDisplayText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="Text displayed here." />

</FrameLayout>

在Logcat中显示以下异常

原因:java.lang.NullPointerException:尝试调用虚拟方法“无效”android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'在空对象引用上在com.example.demo2.MainActivity.onCreate(MainActivity.kt:22)*

android kotlin android-activity fragment communication
2个回答
0
投票

[您要么需要将btnSaveData按钮移至MainActivity中,要么应将onClickListener移至MainFragment中,并进行一些其他更改。类似于:

class MainFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
          // Inflate the layout for this fragment
         val mainInflater = inflater.inflate(R.layout.fragment_main, container, false)
    btnSaveData.setOnClickListener { 

         val secondFragment = SecondFragment()
         activity.supportFragmentManager.beginTransaction().add(R.id.contain_fragment, secondFragment).commit()
        }
        return mainInflater
    }

    fun thisdata():String{

        return "Hello from MainFragment"
    }

}

0
投票

**感谢塔什(Tash),这实际上是有道理的,不幸的是,它仍然显示相同的异常,但我最终通过执行以下操作来解决了该问题。 **

    class MainFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val mainInflater = inflater.inflate(R.layout.fragment_main, container, false)
         mainInflater.btnSaveData.setOnClickListener {

        val secondFragment = SecondFragment()

            secondFragment?.let {
                 activity?.supportFragmentManager?.beginTransaction()?.replace(R.id.contain_fragment, secondFragment)?.commit()
             }

            }
        return mainInflater
    }

    fun thisdata():String{

        return "Hello from MainFragment"
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.