Android Kotlin:java.lang.IllegalStateException [英] Android Kotlin: java.lang.IllegalStateException

查看:167
本文介绍了Android Kotlin:java.lang.IllegalStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 Kotlin使用Gson反序列化本地json文件的扩展我之前发布的.

我希望NewsFragment.kt实例化适配器,但无法访问recyclerview id worldnews.当程序尝试执行以下代码时,我得到"java.lang.IllegalStateException:worldnews不能为空"

I want NewsFragment.kt to instantiate an adapter but am unable to access to the recyclerview id worldnews. I get "java.lang.IllegalStateException: worldnews must not be null" when the program tries to execute the code below:

activity?.runOnUiThread {
    worldnews.adapter = MainAdapter(homeFeed)
}

NewsFragment.kt:

NewsFragment.kt:

class NewsFragment : Fragment() {
    var arr = arrayListOf<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }

        read_json()

    }

    fun read_json(){
        var json : String? = null

        try {
            val inputStream: InputStream = context!!.assets.open("sample.json")

            json = inputStream.bufferedReader().use { it.readText() }

            val gson = GsonBuilder().create()
            val homeFeed = gson.fromJson(json, HomeFeed::class.java)

            activity?.runOnUiThread {
                worldnews.adapter = MainAdapter(homeFeed)
            }

        } catch (e: IOException) {

        }
    }

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

        view.worldnews.layoutManager = LinearLayoutManager(activity)

        return view
    }
}
class HomeFeed(val News: List<News>)

class News(val title: String, val description: String, val time: String, val link: String)

sample.json:

sample.json:

{"News": [{"title": "Intesa expected to approve state-backed loan for FCA -source","description": "Italy's biggest retail bank Intesa Sanpaolo is expected to give conditional approval at a board meeting on Tuesday to a state-guaranteed $6.3 billion euro three-year loan for Fiat Chrysler (FCA), a source close to the matter said.", "time": "9:38am EDT","link": "https://www.reuters.com//article/health-coronavirus-fiat-chrylser-loan/intesa-expected-to-approve-state-backed-loan-for-fca-source-idUSS8N2B200A"}, {"title": "CANADA STOCKS-TSX opens higher on hopes of economic recovery", "description": "Canada's main stock index rose in early trade on Monday as investors looked to an eventual economic recovery from the coronavirus with more countries scaling back lockdown measures.", "time": "9:37am EDT", "link": "https://www.reuters.com//article/canada-stocks/canada-stocks-tsx-opens-higher-on-hopes-of-economic-recovery-idUSL4N2D7257"}, {"title": "Bars, gyms reopen as Iceland exits emergency coronavirus alert", "description": "Iceland eased its national alert against the coronavirus on Monday, allowing for public gatherings of up to 200 people and night clubs and gyms to reopen as the country nears complete recovery from the outbreak.", "time": "9:20am EDT", "link": "https://www.reuters.com//article/health-coronavirus-iceland/bars-gyms-reopen-as-iceland-exits-emergency-coronavirus-alert-idUSL8N2D71YX"}]}

推荐答案

您正在片段的onCreate块中调用read_json()函数,此时您的视图为空.

You are calling your read_json() function in your onCreate block of your fragment, at that point your view is null.

将对read_json的调用从onCreate移到onViewCreated.

Move invocation upon read_json from onCreate to onViewCreated.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
   read_json(view)
}

除了read_json函数中的功能之外,还可以将worldnews修改为view.worldnews:

In addition to that in the read_json function modify worldnews to view.worldnews:

activity?.runOnUiThread {
   view.worldnews.adapter = MainAdapter(homeFeed)
}

这篇关于Android Kotlin:java.lang.IllegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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