在Grid View和Kotlin的List Veiw之间进行连接

我不知道是否有人可以帮我解决这个错误包“不幸的是,启动已停止”我做了3个活动,我想通过“setOnClickListener”,第一个活动的网格视图,第二个列表视图,第三个细节,注意:我的应用程序名称是启动的

这是一个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 

根据LogCat我们可以得出结论,在您的MainActivity2Adapter

  foodView.ivFoodImage2.setImageResource(food.image!!) 

ImageView是空的。

添加一个安全的检查

  foodView.ivFoodImage2?.let{ it.setImageResource(food.image!!) } 

谢谢你mzeus.bolt为你的答案,我做到了,但它显示了同样的包

看错误的错误是在MainActivity2 onCreate方法中使用的ImageView ; 低于给定的行:

 ivFoodImage2.setImageResource(image) 

请用与ImageViewnull的相同的检查,它将如下所示:

 ivFoodImage2?.let{ it.setImageResource(image) } 

尝试调试并设置断点。 也许你忘了最初的东西。 也请提供完整的日志猫在你的问题。