我试图有一个网格布局在一个recyclerview显示和图像文本和双变量。目标是添加一个搜索栏,将结果显示在recycleView中。我是kotlin的新手,如果问题很明显,请原谅我。我不确定究竟是什么导致我的应用程序崩溃,但我缩小到1行代码在我的
MainActivity.kt
dataList.add(mealData("mealTitle.toString()",
Restaurant.toString(),R.drawable.food1,"mealCat.toString()",
"mealDescription.toString()"))
我怀疑这是我的mealCost变量,所以我把它拿出来,但它仍然崩溃,这是我的代码
MainActivity.kt
package com.example.foodme2
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.smarteist.autoimageslider.SliderView
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.food_search_list.*
class MainActivity : AppCompatActivity() {
private lateinit var showImage: ArrayList<Int>
lateinit var sliderView: SliderView
lateinit var sliderAdapter: SliderAdapter
private lateinit var photoAdapter: PhotoAdapter
private var dataList = mutableListOf<mealData>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sliderView = findViewById(R.id.slider)
showImage = ArrayList()
showImage.add(R.drawable.food1)
showImage.add(R.drawable.food3)
showImage.add(R.drawable.food4)
sliderAdapter =SliderAdapter(showImage)
sliderView.autoCycleDirection = SliderView.LAYOUT_DIRECTION_LTR
sliderView.setSliderAdapter(sliderAdapter)
sliderView.scrollTimeInSec = 3
sliderView.isAutoCycle = true
sliderView.startAutoCycle()
recyclerView.layoutManager = GridLayoutManager(applicationContext,2)
photoAdapter = PhotoAdapter()
recyclerView.adapter = photoAdapter
dataList.add(mealData("mealTitle.toString()",
Restaurant.toString(),R.drawable.food1,"mealCat.toString()",
"mealDescription.toString()"))
photoAdapter.setDataList(dataList)
}
}
activityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/Background"
android:layout_margin="2dp"
android:outlineAmbientShadowColor="@color/BorderColor"
tools:context=".MainActivity">
<com.smarteist.autoimageslider.SliderView
android:id="@+id/slider"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_centerInParent="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:sliderAnimationDuration="600"
app:sliderAutoCycleDirection="back_and_forth"
app:sliderIndicatorAnimationDuration="600"
app:sliderIndicatorEnabled="true"
app:sliderIndicatorGravity="center_horizontal|bottom"
app:sliderIndicatorMargin="15dp"
app:sliderIndicatorOrientation="horizontal"
app:sliderIndicatorPadding="3dp"
app:sliderIndicatorRadius="2dp"
app:sliderIndicatorSelectedColor="#5A5A5A"
app:sliderIndicatorUnselectedColor="#FFF"
app:sliderScrollTimeInSec="1" />
<SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/slider" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="406dp"
android:layout_height="400dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchView" />
</androidx.constraintlayout.widget.ConstraintLayout>
recycleview的布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="@color/cadetGrey"
android:layout_margin="2dp"
android:layout_height="match_parent">
<ImageView
android:id="@+id/MealImage"
android:contentDescription="@string/Description"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_marginTop="75dp"
android:background="@color/white"
android:padding="2dp" />
<TextView
android:id="@+id/Restaurant"
android:layout_width="156dp"
android:layout_height="29dp"
android:layout_marginStart="115dp"
android:textAlignment="center"
android:layout_marginTop="8dp"
android:padding="2dp"
android:background="@color/white"
/>
<TextView
android:id="@+id/mealTite"
android:layout_width="100dp"
android:layout_height="20dp"
android:layout_marginStart="140dp"
android:layout_marginTop="44dp"
android:background="@color/white"
android:padding="2dp" />
<TextView
android:id="@+id/mealDescription"
android:layout_width="168dp"
android:layout_height="95dp"
android:layout_marginStart="110dp"
android:layout_marginTop="210dp"
android:background="@color/white"
android:padding="2dp"
android:scaleX="45" />
<TextView
android:id="@+id/mealCost"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="24dp"
android:layout_marginTop="10dp"
android:padding="2dp"
android:background="@color/white"
/>
<TextView
android:id="@+id/mealCat"
android:layout_width="50dp"
android:layout_height="20dp"
android:layout_marginStart="24dp"
android:layout_marginTop="10dp"/>
</androidx.cardview.widget.CardView>
数据类mealData.kt
package com.example.foodme2
import android.widget.TextView
data class mealData(
// var mealId:Int = 0;
var mealName: String,
var restaurant: String,
var mealImage: Int,
var mealCat: String,
var mealDescription: String,
//var mealCost: Double,
)
recycleview的适配器mRecycleViewAdapter.kt
package com.example.foodme2
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
//import kotlinx.android.synthetic.main.food_search_list.view.*
class PhotoAdapter :RecyclerView.Adapter<PhotoAdapter.ViewHolder>() {
private var dataList = emptyList<mealData>()
internal fun setDataList(dataList: List<mealData>) {
this.dataList = dataList
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var mealImage: ImageView = itemView.findViewById(R.id.MealImage)
var mealName: TextView = itemView.findViewById(R.id.mealTite)
var mealCat: TextView = itemView.findViewById(R.id.mealCat)
var restaurant: TextView = itemView.findViewById(R.id.Restaurant)
var mealDescription: TextView = itemView.findViewById(R.id.mealDescription)
//var mealCost: TextView = itemView.findViewById(R.id.mealCost)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.food_search_list, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val data = dataList[position]
holder.mealImage.setImageResource(data.mealImage)
holder.mealName.text = data.mealName
holder.mealDescription.text = data.mealDescription
holder.mealCat.text = data.mealCat
holder.restaurant.text = data.restaurant
//holder.mealCost.text = data.mealCost.toString()
}
override fun getItemCount() = dataList.size
}
And The Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.foodme2">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.FoodMe2">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
和调试器中的s。我试图了解这里发生了什么,我得到的是问题是在我的MainActivity。Kt,这是我第一次开始的代码行。
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.foodme2, PID: 1784
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.foodme2/com.example.foodme2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.widget.TextView.toString()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.widget.TextView.toString()' on a null object reference
at com.example.foodme2.MainActivity.onCreate(MainActivity.kt:43)
at android.app.Activity.performCreate(Activity.java:7994)
at android.app.Activity.performCreate(Activity.java:7978)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I/Process: Sending signal. PID: 1784 SIG: 9
Disconnected from the target VM, address: 'localhost:62990', transport: 'socket'
首先删除:
import kotlinx.android.synthetic.main.food_search_list.*
(并考虑远离Kotlin合成访问器,因为它们已被弃用)
这不是活动的布局;不要尝试使用这些访问器来引用小部件。
然后修改:
dataList.add(mealData("mealTitle.toString()",
Restaurant.toString(),R.drawable.food1,"mealCat.toString()",
"mealDescription.toString()"))
以摆脱对不在此活动中的小部件的引用,并从实际数据填充dataList
。例如,可以这样开头:
dataList.add(mealData("Sample Title",
Restaurant.toString(),R.drawable.food1,"Sample Category",
"Sample Description"))