gpt4 book ai didi

android - Kotlin Android Extensions 提供布局空指针

转载 作者:IT老高 更新时间:2023-10-28 23:32:02 26 4
gpt4 key购买 nike

有一个相当简单的场景给我带来了很多麻烦。我正在使用嵌入式 fragment 制作一个非常简单的 Activity 。这个 fragment 只是一个显示一些图像的 Gridview。当使用 Kotlin 扩展引用 Gridview 以直接引用 XML id 时,就会出现问题。 这里有什么问题? kotlinx 对静态 fragment 不起作用吗?

错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.android_me/com.example.android.android_me.ui.MainActivity}: java.lang.IllegalStateException: gridview_all_parts must not be null
Caused by: java.lang.IllegalStateException: gridview_all_parts must not be null at com.example.android.android_me.ui.MasterListFragment.onActivityCreated(MasterListFragment.kt:22)

带有冒犯性代码行的 fragment

import kotlinx.android.synthetic.main.fragment_master_list.*

class MasterListFragment: Fragment() {

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val layoutView = inflater?.inflate(R.layout.fragment_master_list, container, false)
return layoutView
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
//If this is removed, code runs
gridview_all_parts.adapter = MasterListAdapter(activity, AndroidImageAssets.getAll())
super.onActivityCreated(savedInstanceState)
}
}

fragment 布局:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview_all_parts"
android:layout_width="match_parent" android:layout_height="match_parent"/>

父 Activity 布局

<?xml version="1.0" encoding="utf-8"?>
<!--have tried both class:= and android:name:=-->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
class="com.example.android.android_me.ui.MasterListFragment"
android:id="@+id/fragment_masterlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

家长 Activity

class MainActivity: AppCompatActivity(){

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

最佳答案

要在 Fragment 中使用扩展,您需要使用您的 layoutView .这应该有效:layoutView.gridview_all_parts.adapter = MasterListAdapter(activity, AndroidImageAssets.getAll())

在这种情况下,您可以将 layoutView 设为全局。

更新说明它与 View 膨胀有关。就像在butterknife中一样,我们需要在fragment/recyclerView的情况下绑定(bind)膨胀 View ,在kotlin的情况下,我们需要那个膨胀 View 来访问xml中的 View 。

引自 official documentation ,

Importing synthetic properties It is convenient to import all widget properties for a specific layout in one go:

import kotlinx.android.synthetic.main.<layout>.*

Thus if the layout filename is activity_main.xml, we'd import

kotlinx.android.synthetic.main.activity_main.*.

If we want to call the synthetic properties on View, we should also import

kotlinx.android.synthetic.main.activity_main.view.*.

Once we do that, we can then invoke the corresponding extensions, which are properties named after the views in the XML file.

关于android - Kotlin Android Extensions 提供布局空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48830053/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com