在 kotlin 中连接 Gridview 和 Listview
Posted
技术标签:
【中文标题】在 kotlin 中连接 Gridview 和 Listview【英文标题】:connect between Grid View and List Veiw in kotlin 【发布时间】:2018-02-16 21:04:56 【问题描述】:我想知道是否有人可以帮助我解决这个错误包“不幸的是,启动已停止” 我做了 3 个活动,我想通过“setOnClickListener”在它们之间建立联系, 网格视图的第一个活动, 列表视图的第二个, 第三个细节, 注意:我的应用名称是startup
这是一个 MainActivity.kt
class MainActivity : AppCompatActivity()
var adapter:FoodAdapter?=null
var listOfFoods =ArrayList<Food>()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// load foods
listOfFoods.add(Food("Coffee"," Coffee preparation is",R.drawable.a))
..........
adapter= FoodAdapter(this,listOfFoods)
gvListFood.adapter =adapter
class FoodAdapter:BaseAdapter
var listOfFood= ArrayList<Food>()
var context:Context?=null
constructor(context:Context,listOfFood:ArrayList<Food>):super()
this.context=context
this.listOfFood=listOfFood
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View?
val food = this.listOfFood[p0]
var inflator = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
var foodView= inflator.inflate(R.layout.food_ticket,null)
foodView.ivFoodImage.setImageResource(food.image!!)
foodView.ivFoodImage.setOnClickListener
val intent = Intent(context, Main2Activity::class.java)
intent.putExtra("name", food.name!!)
intent.putExtra("des", food.des!!)
intent.putExtra("image", food.image!!)
context!!.startActivity(intent)
return foodView
override fun getItem(p0: Int): Any
return listOfFood[p0]
override fun getItemId(p0: Int): Long
return p0.toLong()
override fun getCount(): Int
return listOfFood.size
这是一个 Main2Activity.kt
class Main2Activity : AppCompatActivity()
var adapter: FoodAdapter? = null
var listOfFoods2 = ArrayList<Food>()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
val bundle= intent.extras
val name = bundle.getString("name")
val des = bundle.getString("des")
val image = bundle.getInt("image")
ivFoodImage2?.let
it.setImageResource(image)
//ivFoodImage2.setImageResource(image)
tvName2?.let
it.text=name
//tvName2.text=name
tvDes2?.let
it.text=des
//tvDes2.text=des
// load foods
listOfFoods2.add(Food("Coffee"," Coffee preparation is",R.drawable.e))
listOfFoods2.add(Food("Coffee"," Coffee preparation is",R.drawable.f))
listOfFoods2.add(Food("Coffee"," Coffee preparation is",R.drawable.g))
listOfFoods2.add(Food("Coffee"," Coffee preparation is",R.drawable.h))
adapter = FoodAdapter(listOfFoods2, this)
lvFoods2.adapter = adapter
class FoodAdapter : BaseAdapter
var context: Context? = null
var listOfFoodsLocal2 = ArrayList<Food>()
constructor(listOfFoods2: ArrayList<Food>, context: Context) : super()
this.listOfFoodsLocal2 = listOfFoods2
this.context = context
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View
val food = this.listOfFoodsLocal2[p0]
var inflator = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val foodView = inflator.inflate(R.layout.food_ticket2, null)
foodView.tvName2?.let
it.text = food.name!!
//foodView.tvName2.text = food.name!!
foodView.tvDes2?.let
it.text = food.des!!
//foodView.tvDes2.text = food.des!!
foodView.ivFoodImage2?.let
it.setImageResource(food.image!!)
//foodView.ivFoodImage2.setImageResource(food.image!!)
foodView.ivFoodImage2.setOnClickListener
//move to next
val intent = Intent(context, FoodDetails::class.java)
intent.putExtra("name", food.name!!)
intent.putExtra("des", food.des!!)
intent.putExtra("image", food.image!!)
context!!.startActivity(intent)
return foodView
override fun getItem(p0: Int): Any
return listOfFoodsLocal2[p0]
override fun getItemId(p0: Int): Long
return p0.toLong()
override fun getCount(): Int
return listOfFoodsLocal2.size
这是一个 FoodDetails.kt
class FoodDetails : AppCompatActivity()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_food_details)
val bundle= intent.extras
val name = bundle.getString("name")
val des = bundle.getString("des")
val image = bundle.getInt("image")
ivFoodImage3.setImageResource(image)
tvName3.text=name
tvDetails3.text=des
这是一个 Food.kt(用于 3 个对象)
package com.marzadmz.startup
class Food
var name:String? =null
var des:String? =null
var image:Int? =null
constructor(name:String,des:String,image:Int)
this.name=name
this.des=des
this.image=image
这是我的 Logcat
HostConnection::get() New Host Connection established 0xeec39450, tid 5472
09-09 14:16:11.338 5453-5472/com.marzadmz.startup I/OpenGLRenderer: Initialized EGL, version 1.4
09-09 14:16:11.413 5453-5472/com.marzadmz.startup D/OpenGLRenderer: Enabling debug mode 0
09-09 14:16:11.448 5453-5472/com.marzadmz.startup W/EGL_emulation: eglSurfaceAttrib not implemented
09-09 14:16:11.449 5453-5472/com.marzadmz.startup W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xeec35780, error=EGL_SUCCESS
09-09 14:16:11.557 5453-5468/com.marzadmz.startup I/art: Background sticky concurrent mark sweep GC freed 92(3KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 18MB/18MB, paused 25.260ms total 52.920ms
09-09 14:16:11.590 5453-5468/com.marzadmz.startup I/art: Background partial concurrent mark sweep GC freed 46(1952B) AllocSpace objects, 0(0B) LOS objects, 40% free, 18MB/30MB, paused 14.620ms total 31.382ms
09-09 14:16:11.686 5453-5468/com.marzadmz.startup I/art: Background sticky concurrent mark sweep GC freed 43(1408B) AllocSpace objects, 0(0B) LOS objects, 0% free, 72MB/72MB, paused 28.910ms total 31.763ms
09-09 14:16:11.965 5453-5453/com.marzadmz.startup I/Choreographer: Skipped 53 frames! The application may be doing too much work on its main thread.
09-09 14:16:53.756 5453-5472/com.marzadmz.startup W/EGL_emulation: eglSurfaceAttrib not implemented
09-09 14:16:53.756 5453-5472/com.marzadmz.startup W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xf3f5baa0, error=EGL_SUCCESS
09-09 14:16:53.793 5453-5453/com.marzadmz.startup I/art: Alloc partial concurrent mark sweep GC freed 209(15KB) AllocSpace objects, 0(0B) LOS objects, 18% free, 72MB/88MB, paused 560us total 7.402ms
09-09 14:16:53.804 5453-5453/com.marzadmz.startup I/art: Alloc concurrent mark sweep GC freed 214(29KB) AllocSpace objects, 0(0B) LOS objects, 18% free, 72MB/88MB, paused 1.062ms total 10.928ms
09-09 14:16:53.804 5453-5453/com.marzadmz.startup I/art: Forcing collection of SoftReferences for 122MB allocation
09-09 14:16:53.814 5453-5453/com.marzadmz.startup I/art: Alloc concurrent mark sweep GC freed 9(248B) AllocSpace objects, 0(0B) LOS objects, 18% free, 72MB/88MB, paused 541us total 9.879ms
09-09 14:16:53.815 5453-5453/com.marzadmz.startup E/art: Throwing OutOfMemoryError "Failed to allocate a 128043252 byte allocation with 16777216 free bytes and 23MB until OOM"
09-09 14:16:53.825 5453-5453/com.marzadmz.startup I/art: Alloc partial concurrent mark sweep GC freed 7(240B) AllocSpace objects, 0(0B) LOS objects, 18% free, 72MB/88MB, paused 807us total 6.145ms
09-09 14:16:53.834 5453-5453/com.marzadmz.startup I/art: Alloc concurrent mark sweep GC freed 4(144B) AllocSpace objects, 0(0B) LOS objects, 18% free, 72MB/88MB, paused 1.097ms total 9.388ms
09-09 14:16:53.834 5453-5453/com.marzadmz.startup I/art: Forcing collection of SoftReferences for 122MB allocation
09-09 14:16:53.844 5453-5453/com.marzadmz.startup I/art: Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 18% free, 72MB/88MB, paused 437us total 9.054ms
09-09 14:16:53.844 5453-5453/com.marzadmz.startup E/art: Throwing OutOfMemoryError "Failed to allocate a 128043252 byte allocation with 16777120 free bytes and 23MB until OOM"
09-09 14:16:53.844 5453-5453/com.marzadmz.startup D/skia: --- decoder->decode returned false
09-09 14:16:53.844 5453-5453/com.marzadmz.startup D/androidRuntime: Shutting down VM
09-09 14:16:53.845 5453-5453/com.marzadmz.startup E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.marzadmz.startup, PID: 5453
java.lang.OutOfMemoryError: Failed to allocate a 128043252 byte allocation with 16777120 free bytes and 23MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2474)
at android.content.res.Resources.loadDrawable(Resources.java:2381)
at android.content.res.Resources.getDrawable(Resources.java:787)
at android.content.Context.getDrawable(Context.java:403)
at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:30)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:372)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
at android.support.v7.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:54)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:66)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:56)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.marzadmz.startup.Main2Activity$FoodAdapter.getView(Main2Activity.kt:69)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.ListView.makeAndAddView(ListView.java:1864)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillFromTop(ListView.java:759)
at android.widget.ListView.layoutChildren(ListView.java:1673)
at android.widget.AbsListView.onLayout(AbsListView.java:2151)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(Linea
【问题讨论】:
请在您的问题中提供完整的日志猫 【参考方案1】:根据 LogCat,我们可以得出结论,在 MainActivity2
中的 Adapter
类中
foodView.ivFoodImage2.setImageResource(food.image!!)
ImageView
为空。
添加安全检查
foodView.ivFoodImage2?.let
it.setImageResource(food.image!!)
查看错误,错误在于您在onCreate
方法中使用的ImageView
MainActivity2
,即;下面给定的行:
ivFoodImage2.setImageResource(image)
请检查 ImageView
是 null
,它会是这样的:
ivFoodImage2?.let
it.setImageResource(image)
【讨论】:
谢谢你 mzeus.bolt,请你检查我的图片,因为它显示了相同的错误包 我正在尝试调试,我在“Main2Activity”类中发现了此消息,请检查我的 Logcat 是否已更新,消息为:来自视图适配器的无条件布局膨胀:应使用 View Holder 模式更多信息: [developer.android.com/training/improving-layouts/… 它与您之前发布的完全不同的错误。它的内存不足错误。您需要缩放图像【参考方案2】:尝试调试并设置断点。也许你忘记了初始化一些东西。另请在您的问题中提供完整的日志猫。
【讨论】:
以上是关于在 kotlin 中连接 Gridview 和 Listview的主要内容,如果未能解决你的问题,请参考以下文章
Android kotlin 用RecyclerView快速实现类似于旧版GridView的功能(附源码)
Android kotlin 用RecyclerView快速实现类似于旧版GridView的功能(附源码)