我從 Logcat 得到這個
2022-01-07 20:27:46.539 14327-14327/com.example.donedoobnew2 E/AndroidRuntime:致命例外:主行程:com.example.donedoobnew2,PID:14327 java.lang.NullPointerException:嘗試呼叫虛擬方法' void android.widget.ListView.setAdapter(android.widget.ListAdapter)' 在 com.example.donedoobnew2.Fragments.FragmentMinistries.onCreateView(FragmentMinisteries.kt:29) 的空物件參考上
片段部委.kt
class FragmentMinistries : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
val customListData = ArrayList<CList>()
val customList = CListAdapter(this.requireContext(), customListData)
customListData.add(CList(R.drawable.ic_call, "First"))
customListData.add(CList(R.drawable.ic_call, "Second"))
customListData.add(CList(R.drawable.ic_call, "Third"))
lv_donedoob_ministries.adapter = customList
return LayoutInflater.from(container?.context)
.inflate(R.layout.fragment_ministeries, container, false)
}
}
CListAdapter.kt
class CListAdapter(private val getContext: Context, private val customListItem: ArrayList<CList>) :
ArrayAdapter<CList>(getContext, 0, customListItem) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var listLayout = convertView
var holder: ViewHolder
if (listLayout == null) {
val inflateList = (getContext as Activity).layoutInflater
listLayout = inflateList.inflate(R.layout.item_cardview_donedoob, parent, false)
holder = ViewHolder()
holder.mTextView = listLayout!!.findViewById(R.id.tv_card_donedoob)
holder.mImageListView = listLayout.findViewById(R.id.iv_card_donedoob)
listLayout.tag = holder
} else {
holder = listLayout.tag as ViewHolder
}
val listItem = customListItem[position]
holder.mImageListView!!.setImageResource(listItem.mCListImage)
holder.mTextView!!.text = listItem.mCListText
return listLayout
}
class ViewHolder {
internal var mImageListView: ImageView? = null
internal var mTextView: TextView? = null
}
}
CList.kt
class CList(
var mCListImage: Int,
var mCListText: String,
)
片段部委.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray"
tools:context=".Fragments.FragmentMinistries">
<ListView
android:id="@ id/lv_donedoob_ministries"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="10dp"
android:divider="@color/gray"
android:padding="20dp">
</ListView>
</FrameLayout>
item_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/round_buttons"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@ id/iv_card_donedoob"
android:layout_width="180dp"
android:layout_height="150dp"
android:src="@drawable/icon_my_documents" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<TextView
android:id="@ id/tv_card_donedoob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ministry og edu"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
uj5u.com熱心網友回復:
在這段代碼中:
lv_donedoob_ministries.adapter = customList
return LayoutInflater.from(container?.context)
.inflate(R.layout.fragment_ministeries, container, false)
您正在嘗試在ListView擴展布局之前在(lv_donedoob_ministries)上設定配接器。您需要充氣布局首先嘗試設定配接器上它。
uj5u.com熱心網友回復:
您正在嘗試在膨脹片段視圖之前訪問 ListView。
class FragmentMinistries : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
val customListData = ArrayList<CList>()
val customList = CListAdapter(this.requireContext(), customListData)
customListData.add(CList(R.drawable.ic_call, "First"))
customListData.add(CList(R.drawable.ic_call, "Second"))
customListData.add(CList(R.drawable.ic_call, "Third"))
val view = LayoutInflater.from(container?.context)
.inflate(R.layout.fragment_ministeries, container, false)
view.findViewById<ListView>(R.id.lv_donedoob_ministries).adapter = customList
return view
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/405212.html
標籤:
