android - 如何在 android studio 中用新 fragment 替换已经打开的 fragment ?

标签 android android-fragments kotlin

我正在尝试用 PickPowerFragement fragment 替换 MainFragment fragment ,但是当我按下 MainFragment 中的按钮以用 PickPowerFragment 替换 MainFragment 时,应用程序崩溃了。 我能够按照教程中的说明在 java 中成功加载 PickPowerFragment,但是当我尝试在 Kotlin 中这样做时应用程序崩溃(仅用于练习)。 那么如何使用 Kotlin 语法替换 Fragment

activity_hero_me.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.mandar.herome.Activities.HeroMe"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="25dp">

    <FrameLayout
        android:id="@+id/ReplaceFrame"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

    </FrameLayout>
</android.support.constraint.ConstraintLayout>

HeroMe.kt

package com.example.mandar.herome.Activities

import android.app.Activity
import android.app.Fragment
import android.app.FragmentManager
import android.net.Uri
import android.os.Bundle
import com.example.mandar.herome.Fragments.MainFragment
import com.example.mandar.herome.Fragments.PickPowerFragment
import com.example.mandar.herome.R


class HeroMe : Activity() , MainFragment.OnMainFragmentInteractionListener , PickPowerFragment.pickPowerInteractionListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_hero_me)
        val manager : FragmentManager = fragmentManager
        var fragment : Fragment? = manager.findFragmentById(R.id.ReplaceFrame)
        if (fragment == null){
            fragment = MainFragment()
            manager.beginTransaction().add(R.id.ReplaceFrame,fragment).commit()
        }
    }

    fun loadPickPowerScreen(){
            var pickpowerfrag = PickPowerFragment()
            fragmentManager.beginTransaction().replace(R.id.ReplaceFrame, pickpowerfrag).addToBackStack(null).commit()
    }

    override fun onMainFragmentInteraction(uri: Uri) {

    }

    override fun onPickPowerInteraction(uri: Uri) {

    }

}

这里我从 MainFragment 调用函数 loadPickPowerScreen()

我测试了按钮(我用来用 pickPowerFragment 替换 MainFragment 的按钮)是否正常工作,方法是在单击按钮时使用它修改上面的文本,它似乎工作得很好。所以我猜MainFragment.kt 应该没有问题

这是 MainFragment.kt 中的 onClick() 方法

var onClick = View.OnClickListener(){view ->
    var heroActivity : HeroMe = HeroMe()
    heroActivity.loadPickPowerScreen()
}

这里是 fragment PickPowerFragment.kt

package com.example.mandar.herome.Fragments

import android.app.Fragment
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.mandar.herome.R

/**
 * A simple [Fragment] subclass.
 * Activities that contain this fragment must implement the
 * [PickPowerFragment.OnFragmentInteractionListener] interface
 * to handle interaction events.
 * Use the [PickPowerFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class PickPowerFragment : Fragment() {

    // TODO: Rename and change types of parameters
    private var mParam1: String? = null
    private var mParam2: String? = null

    private var mListener: pickPowerInteractionListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (arguments != null) {
            mParam1 = arguments.getString(ARG_PARAM1)
            mParam2 = arguments.getString(ARG_PARAM2)
        }
    }

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

    // TODO: Rename method, update argument and hook method into UI event
    fun onButtonPressed(uri: Uri) {
        if (mListener != null) {
            mListener!!.onPickPowerInteraction(uri)
        }
    }

    override fun onAttach(context: Context?) {
        super.onAttach(context)
        if (context is pickPowerInteractionListener) {
            mListener = context
        } else {
            throw RuntimeException(context!!.toString() + " must implement OnFragmentInteractionListener")
        }
    }

    override fun onDetach() {
        super.onDetach()
        mListener = null
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     *
     *
     * See the Android Training lesson [Communicating with Other Fragments](http://developer.android.com/training/basics/fragments/communicating.html) for more information.
     */
    interface pickPowerInteractionListener {
        // TODO: Update argument type and name
        fun onPickPowerInteraction(uri: Uri)
    }

    companion object {
        // TODO: Rename parameter arguments, choose names that match
        // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
        private val ARG_PARAM1 = "param1"
        private val ARG_PARAM2 = "param2"

        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment PickPowerFragment.
         */
        // TODO: Rename and change types and number of parameters
        fun newInstance(param1: String, param2: String): PickPowerFragment {
            val fragment = PickPowerFragment()
            val args = Bundle()
            args.putString(ARG_PARAM1, param1)
            args.putString(ARG_PARAM2, param2)
            fragment.arguments = args
            return fragment
        }
    }
}// Required empty public constructor

崩溃后的 logcat: Logcat after the crash:

调试器在崩溃前提供的信息: debugger variable values just before crash

这里我为项目添加 Git Repository 链接 Git Android Project: HeroMe

最佳答案

经过 2 天的挖掘,我终于找到了解决问题的方法。 在将 Java 转换为 Kotlin 时,我在 onClick 方法中犯了一个错误,导致崩溃。

代替:

var onClick = View.OnClickListener(){view ->
    var heroActivity : HeroMe = HeroMe()
    heroActivity.loadPickPowerScreen()
}

代码应该是:

var onClick = View.OnClickListener(){view ->
    var heroActivity : HeroMe= activity as HeroMe
    heroActivity.loadPickPowerScreen()
}

感谢所有试图帮助我的人。


说明此代码为何有效:

当你写作时,

var heroActivity : HeroMe = HeroMe()

因为构造函数,它实际上创建了其他对象( Activity ),这与实际加载的 Activity 不同。所以这个新 Activity 被创建但没有显示,因为没有调用 startActivity 方法。所以,

heroActivity.loadPickPowerScreen()

此方法在未启动的 Activity 上调用。

现在,这条线

var heroActivity : HeroMe= activity as HeroMe

类似于

getActivity()

form java,返回fragment的父activity。 返回类型是 Activity,因此我们需要对其进行区分,以便我们可以调用该 Activity 中的函数。

关于android - 如何在 android studio 中用新 fragment 替换已经打开的 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46525366/

相关文章:

android - android 中用于在社交媒体中共享的公共(public)库?

android - 如何从命令行设置模拟器像素密度?

android - 指定的 child 已经有一个 parent ,RecyclerView

android - 恢复 Activity 时重叠 fragment

android - 如何从 SQLite 的列表 fragment 中获取我的 ListView 类?

android - 如何在Android中从Fragment传递Context对象?

android - 如何为 Android fragment 中的多个按钮定义 setOnTouchListener

Kotlin:为什么 Sequence 在这个例子中性能更高?

android - Kotlin:尝试将以下函数添加到全局共享文件以在多个 Activity 中使用,但不断收到不同的错误

reflection - 我可以在纯 Kotlin 中通过类名实例化一个对象吗