Kotlin从其主要活动通过的片段之间传递数据为null [英] Kotlin pass data between fragment from it main activity is null

查看:248
本文介绍了Kotlin从其主要活动通过的片段之间传递数据为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难将数据从主要活动传递到其片段. 这里的主要活动是ProductActivity.class,它使用Kotlin使用tablayout具有两个片段

I having difficulties to pass data from main activity to its fragment. Here main activity is ProductActivity.class that have two fragment using tablayout using Kotlin

使用bundle并将其设置为参数似乎不被编译器视为错误,因为在创建新片段之后字符串未保存或可能为null.

Using bundle and set to argument seem not working as error by compiler because string was not save or maybe null after create new fragment.

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
                                                                      at my.hipi.hipiboutique.fragments.ProductPageFragment.onCreateView(ProductPageFragment.kt:22)

出于这个问题,我仅使用此字符串示例在两个片段之间传递,但基本上我使用的是json字符串.

i'm using this string example only to pass between two fragment for this question purpose but basically i'm using the the json string.

ProductActivity

ProductActivity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_product.*
import my.hipi.hipiboutique.fragments.ProductPageFragment
import java.util.ArrayList

class ProductActivity : AppCompatActivity() {

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

        val product1 = this.intent.getStringExtra("array")
        val fullProduct = Gson().fromJson(product1, Product::class.java)

        val testString : String = "example only"
        var bundle = Bundle()
        bundle.putString("test", testString)
        var frag = ProductPageFragment()
        frag.arguments = bundle

        viewPager_fragment.adapter = TabsAdapter(supportFragmentManager)
        tabLayout.setupWithViewPager(viewPager_fragment)

    }


}

这是TabsAdapter

this is TabsAdapter

import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import my.hipi.hipiboutique.fragments.ProductDetailFragment
import my.hipi.hipiboutique.fragments.ProductPageFragment

class TabsAdapter(manager: FragmentManager) : FragmentPagerAdapter (manager) {

    private val fragmentTitles = arrayOf("Product", "Details", "Contacts")

    override fun getItem(position: Int): Fragment {
        return when (position) {
            0 ->  ProductPageFragment()
            1 ->  ProductDetailFragment()
            //2 ->  ContactsFragment()
            else ->  ProductPageFragment()
        }
    }

    override fun getCount(): Int {
         return 3
    }

    override fun getPageTitle(position: Int): CharSequence {
        return fragmentTitles[position]
    }

}

这是ProductPageFragemnt.class

this is ProductPageFragemnt.class

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import my.hipi.hipiboutique.R

class ProductPageFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment

        var gt = this.arguments.getString("test")
        println(gt)

        return inflater!!.inflate(R.layout.fragment_product_page, container, false)  
    }
}

如何将此数据字符串传递给两个片段.是可以传递给一个片段,但是如果转到第二个选项卡,将导致空数据.我希望这两个片段都可以使用这些数据.谢谢.

How to pass this data string to both fragments. Yes can pass to one fragment but if go to second tab,will cause null data. I want this data available to both fragments .Thank in advance.

推荐答案

在您的ProductActivity中,您正在创建一个片段并设置其参数,但是您对该片段不做任何事情.它只是被扔掉了:

In your ProductActivity, you're creating a fragment and setting its arguments, but you're not doing anything with that fragment. It just gets thrown away:

val testString : String = "example only"
var bundle = Bundle()
bundle.putString("test", testString)
var frag = ProductPageFragment()
frag.arguments = bundle 

在您的TabsAdapter中,创建了实际使用的片段,但从未设置它们的参数:

In your TabsAdapter, you create the fragments that actually get used, but you never set their arguments:

override fun getItem(position: Int): Fragment {
    return when (position) {
        0 ->  ProductPageFragment()
        1 ->  ProductDetailFragment()
        //2 ->  ContactsFragment()
        else ->  ProductPageFragment()
    }
}

您可以删除代码的第一部分,但是您希望在适配器中应用相同的逻辑.也许是这样的:

You can delete the first section of code, but you'll want to apply that same logic in your adapter. Maybe something like this:

override fun getItem(position: Int): Fragment {
    return when (position) {
        0 -> {
            var bundle = Bundle()
            bundle.putString("test", "some string here")
            var frag = ProductPageFragment()
            frag.arguments = bundle
            frag
        }
        ...
    }
}

这篇关于Kotlin从其主要活动通过的片段之间传递数据为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆