如何用android studio中的新片段替换已打开的片段?

时间:2017-10-02 12:21:28

标签: android android-fragments kotlin

我正在尝试使用PickPowerFragement片段替换MainFragment片段,但是当我按下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()

我测试了如果按钮(我用来替换MainFragment和pickPowerFragment的按钮)可以正常使用它,当点击按钮时它可以修改它上面的文本,它似乎工作得很好。所以我想那里不应该是MainFragment.kt

的任何问题

这是MainFragment.kt的onClick()方法

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

这里是片段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

4 个答案:

答案 0 :(得分:3)

你不能像这样实例化Activity并调用类方法......(没办法在java中工作)

  

var heroActivity:HeroMe = HeroMe()
  heroActivity.loadPickPowerScreen()

首先将方法loadPickPowerScreen()添加到MainFragment.OnMainFragmentInteractionListener,然后调用
var onClick = View.OnClickListener(){view -> mListener.loadPickPowerScreen() }
我假设MainFragmentPickPowerFragmentonAttach

相似onDetach

This link正是您所寻求的,请尝试遵循一些编码准则:)

编辑

HeroMe活动中你有:

override fun onMainFragmentInteraction(uri: Uri) {
    var pickpowerfrag = PickPowerFragment()
    fragmentManager.beginTransaction()
        .replace(R.id.ReplaceFrame, pickpowerfrag)
        .addToBackStack(null)
        .commit()
}

然后在OnClickListener

var onClick = View.OnClickListener(){view ->
    mListener?.onMainFragmentInteraction(someUri)
}

答案 1 :(得分:3)

经过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()

因为构造函数它实际上创建了与实际加载的活动不同的其他对象(活动)。因此,创建此新活动但未显示,因为未调用startActivity方法。所以,

  

heroActivity.loadPickPowerScreen()

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

现在,这一行

  

var heroActivity:HeroMe =活动为HeroMe

类似于

  

getActivity()

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

答案 2 :(得分:2)

fun replaceFragment(fragment: Fragment) {
    mFragmentStack = Stack()
    val transaction = mFragmentManager?.beginTransaction()
    transaction?.replace(R.id.layout_frame_content, fragment)
    (mFragmentStack as Stack<Fragment>).push(fragment)
    transaction?.commitAllowingStateLoss()
}



fun addFragment(fragment: Fragment) {
    if (mFragmentManager != null) {
        val transaction = (mFragmentManager as FragmentManager).beginTransaction()
        transaction.add(R.id.layout_frame_content, fragment)
        mFragmentStack?.lastElement()?.onPause()
        transaction.hide(mFragmentStack?.lastElement())
        mFragmentStack?.push(fragment)
        transaction.commitAllowingStateLoss()
    }
}

上述方法可用于替换和添加片段到自定义片段堆栈和片段管理器

答案 3 :(得分:1)

用于添加片段

  getSupportFragmentManager().beginTransaction().add(R.id.container,FragmentLogin()).commit();

用于片段替换

 getSupportFragmentManager().beginTransaction().replace(R.id.container,FragmentLogin()).commit()