我正在处理的应用程序会加载一个片段并将其呈现给activity_main.xml,效果很好。问题是我实现了RecyclerView和CardView,以便动态列出我的技能。出现如下错误:
java.lang.IllegalStateException:
BitmapFactory.decodeResource(ic_launcher_foreground) must not be null
此代码位于Kotlin片段中,该片段加载了相应的片段( SkillFragment )
package smartline.jjba.storyofmylife
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import smartline.jjba.storyofmylife.R.id.rv
import androidx.recyclerview.widget.LinearLayoutManager
class SkillFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_skills, container, false)
companion object {
fun newInstance(): SkillFragment = SkillFragment()
}
fun addItemsToCard(items: ArrayList<MyData>) {
items.run {
add(MyData("text 1", BitmapFactory.decodeResource(resources, R.drawable.ic_launcher_foreground)))
add(MyData("text 2", BitmapFactory.decodeResource(resources, R.drawable.ic_baseline_bar_chart_24px)))
add(MyData("text 3", BitmapFactory.decodeResource(resources, R.drawable.ic_baseline_bar_chart_24px)))
add(MyData("text 4", BitmapFactory.decodeResource(resources, R.drawable.ic_baseline_bar_chart_24px)))
add(MyData("text 5", BitmapFactory.decodeResource(resources, R.drawable.ic_baseline_bar_chart_24px)))
add(MyData("text 6", BitmapFactory.decodeResource(resources, R.drawable.ic_baseline_bar_chart_24px)))
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val _recyclerView: RecyclerView = view.findViewById(R.id.rv)
_recyclerView.layoutManager = LinearLayoutManager(this.context, LinearLayout.VERTICAL, false)
var itemList = ArrayList<MyData>()
addItemsToCard(itemList)
//creating our adapter
val adapter = RecyclerViewAdapter(itemList)
//now adding the adapter to recyclerview
_recyclerView.adapter = adapter
}
}
我尝试了几种不同的可绘制资源,但无济于事。我也很高兴知道我是否可以调用另一种方法来将可绘制对象渲染到位图中
这是我的数据类
package smartline.jjba.storyofmylife
import android.graphics.Bitmap
data class MyData(var text:String, var image: Bitmap)
这是我的适配器代码
class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
fun bindItems(data : MyData){
val _textView:TextView = itemView.findViewById(R.id.textview)
val _imageView:ImageView = itemView.findViewById(R.id.imageview)
_textView.text = data.text
_imageView.setImageBitmap(data.image)
//set the onclick listener for the singlt list item
itemView.setOnClickListener({
Log.e("ItemClicked", data.text)
})
}
}
我在寻找什么
提供有关如何解决此问题的建议,或者提供有关是否是Android或Kotlin中的已知错误的信息。我可能可以在设备上重新安装Android Studio,虽然可以,但是我可以阻止它再次发生,但是我不希望发生这种情况。
这是例外日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: smartline.jjba.storyofmylife, PID: 21687
java.lang.IllegalStateException: BitmapFactory.decodeReso…e.ic_launcher_foreground) must not be null
at smartline.jjba.storyofmylife.SkillFragment.addItemsToCard(SkillFragment.kt:28)
at smartline.jjba.storyofmylife.SkillFragment.onViewCreated(SkillFragment.kt:44)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1471)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
感谢提前感谢这个伟大的社区!
更新
感谢@Kiskae不再例外! 在位图中添加一个问号,以进行Kotlin空检查
data class MyData(var text:String, var image: Bitmap?)
但未加载任何图像,并且第一项也未加载?
答案 0 :(得分:2)
BitmapFactory.decodeResource
的文档指出,这不是Android和Kotlin中的错误,
已解码的位图;如果无法解码图像数据,则为null;或者,如果opts为非null,则如果opts请求仅返回大小(在opts.outWidth和opts.outHeight中)
由于您已定义MyData
来容纳非空 Bitmap
,因此kotlin将插入检查以确保返回的位图不为null,因此例外发生。