【问题标题】:No adapter attached; skipping layout error in logcat windowin android studio未连接适配器;在 android studio 中的 logcat 窗口中跳过布局错误
【发布时间】:2023-04-10 20:53:02
【问题描述】:

我从 3 到 4 天一直面临这个问题,但我仍然无法解决这个错误。我也经历过这个a solution of the same problem

但我还不够了解。我明白了,当我们没有连接适配器但我已经连接时会发生此错误。 下面我附上了我的片段文件、适配器文件和 RecyclerView 文件-

这是我声明我的适配器的代码

import android.content.Context
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 com.example.chumonsuru.R
import com.example.chumonsuru.model.Resturant
import com.squareup.picasso.Picasso
class DashboardRecyclerAdapter(val context : Context , val resturantInfoList : ArrayList<Resturant>) : RecyclerView.Adapter<DashboardRecyclerAdapter.DashboardViewHolder>(){
   class DashboardViewHolder(view:View): RecyclerView.ViewHolder(view){
       val txtResturantid : TextView = view.findViewById(R.id.txtResturantid)
       val txtResturantName : TextView = view.findViewById(R.id.txtResturantName)
       val txtPerPrice : TextView = view.findViewById(R.id.txtPerPrice)
       val txtResturantRating : TextView = view.findViewById(R.id.txtResturantRating)
       val imgResturantImage : ImageView = view.findViewById(R.id.imgResturantImage)
   }
   override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DashboardViewHolder {
       val view = LayoutInflater.from(parent.context).inflate(R.layout.recycler_dashboard_row_one , parent , false)
       return DashboardViewHolder(view)
   }
   override fun getItemCount(): Int {
       return resturantInfoList.count()
   }
   override fun onBindViewHolder(holder: DashboardViewHolder, position: Int) {

       val resturant = resturantInfoList[position]
       holder.txtResturantid.text = resturant.id
       holder.txtResturantName.text = resturant.name
       holder.txtPerPrice.text = resturant.cost_for_one
       holder.txtResturantRating.text = resturant.rating
       Picasso.get().load(resturant.image_url).into(holder.imgResturantImage)

   }
} 

这是我的仪表板片段文件-


import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.example.chumonsuru.R
import com.example.chumonsuru.adapter.DashboardRecyclerAdapter
import com.example.chumonsuru.model.Resturant
import kotlin.collections.HashMap


class DashFrag : Fragment() {
   lateinit var recyclerView: RecyclerView
   lateinit var linearLayoutManager: LinearLayoutManager
   lateinit var recyclerAdapter: DashboardRecyclerAdapter

   val resturantInfoList = arrayListOf<Resturant>()

   override fun onCreateView(

           inflater: LayoutInflater, container: ViewGroup?,
           savedInstanceState: Bundle?
   ): View? {
       // Inflate the layout for this fragment
       val view = inflater.inflate(R.layout.dash_frag, container, false)

       linearLayoutManager = LinearLayoutManager(activity)
       recyclerView = view.findViewById(R.id.recyclerView)
       val queue = Volley.newRequestQueue(activity as Context)

       val url = "http://13.235.250.119/v1/book/fetch_books/"
       val jsonObjectRequest = object : JsonObjectRequest(Request.Method.GET, url, null, Response.Listener {

           val success = it.getBoolean("success")

           if (success != null) {
               val data = it.getJSONArray("data")
               for (i in 0 until data.length()) {
                   val resturantJsonObject = data.getJSONObject(i)
                   val resturantObject = Resturant(

                           resturantJsonObject.getString("id"),
                           resturantJsonObject.getString("name"),
                           resturantJsonObject.getString("rating"),
                           resturantJsonObject.getString("cost_for_one"),
                           resturantJsonObject.getString("image_url")
                   )
                   resturantInfoList.add(resturantObject)
                   recyclerAdapter = DashboardRecyclerAdapter(activity as Context, resturantInfoList)
                   recyclerView.adapter = recyclerAdapter
                   recyclerView.layoutManager = linearLayoutManager
                   recyclerView.addItemDecoration(DividerItemDecoration(recyclerView.context,
                           (linearLayoutManager)
                                   .orientation))
               }


           }


       },
               Response.ErrorListener {

                   /////ERROR/////

               }) {

           override fun getHeaders(): MutableMap<String, String> {
               val headers = HashMap<String, String>()
               headers["Content-type"] = "application/json"
               headers["token"] = "xyz"
               return headers
           }
       }

       queue.add(jsonObjectRequest)
       return view
   }
}

和 我的餐厅课-


data class Resturant(
        val id: String,
        val name: String,
        val rating: String,
        val cost_for_one : String,
        val image_url: String
)


我的仪表板片段 xml 文件-

<RelativeLayout 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"
    tools:context=".fragments.DashFrag">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/txtHelloFrag"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_blank_fragment"
        android:textSize="50sp"
        android:textColor="@color/colorAccent"
        android:padding="20dp"
        android:background="@drawable/gradient"/>
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtHelloFrag"
        android:layout_margin="10dp"
        android:padding="5dp"
        />

</RelativeLayout>

以及recycler view的行布局

<?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="140dp"
android:orientation="horizontal"
android:background="#ffffff"
android:weightSum="6">



<ImageView
    android:layout_weight="1.5"
    android:id="@+id/imgResturantImage"
    android:layout_width="0dp"
    android:layout_height="110dp"
    android:src="@mipmap/ic_launcher1"
    android:padding="5dp"/>

<RelativeLayout
    android:layout_weight="3.3"
    android:layout_width="0dp"
    android:layout_height="match_parent">
    <TextView
        android:padding="8dp"
        android:id="@+id/txtResturantid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="id"
        android:layout_toLeftOf="@id/txtResturantName"
        android:textSize = "18sp"
        />
    <TextView
        android:id="@+id/txtResturantName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Name of the Resturant"
        android:paddingTop="8dp"
        android:textSize="18sp"
        android:textColor="#000000"/>

    <TextView
        android:id="@+id/txtBookAuthor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtResturantName"
        android:text="Name of the Author"
        android:padding="8dp"
        android:textSize="15sp"/>

    <TextView
        android:id="@+id/txtPerPrice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Rs. 299"
        android:paddingTop="8dp"
        android:paddingLeft="8dp"
        android:layout_below="@id/txtBookAuthor"
        android:textSize="15sp"
        android:textStyle="bold"
        android:textColor="#357a38"/>
    <TextView
        android:id="@+id/txtPerPerson"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="/person"
        android:layout_toRightOf="@id/txtPerPrice"
        android:textSize="15sp"
        android:layout_below="@id/txtBookAuthor"
        android:textColor="#357a38"
        android:paddingTop="8dp"
        android:textStyle="bold"/>
</RelativeLayout>


<TextView
    android:id="@+id/txtResturantRating"
    android:layout_weight="1.2"
    android:layout_width="0dp"
    android:padding="6dp"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/star_foreground"
    android:textColor="#ffca28"
    android:text="4.5"
    android:textSize="15sp"
    android:textStyle="bold">

</TextView>

</LinearLayout>

我想我已经将适配器附加到我的回收站视图中。如果我没有请帮我这样做。 请帮我解决这个问题

【问题讨论】:

    标签: android-studio kotlin android-fragments android-recyclerview android-adapter


    【解决方案1】:

    我认为你需要检查一些东西。

     return view
    

    除了这个,你必须使用

     return view;
    

    秒,

    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/txtHelloFrag"
            android:layout_margin="10dp"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            android:padding="5dp"
            />
    

    您尝试将app:layoutManager 放入其中。

     val layoutManager = LinearLayoutManager(this)
     recyclerView_main.setLayoutManager(layoutManager)
    

    如上,在setLayoutManager中设置了LayoutManager,它作为代码处理。

     LinearLayoutManager manager = new LinearLayoutManager(this);
     recyclerView.setLayoutManager(manager);
    

    这个是上面相关的,你已经为RecyclerView设置了布局管理器

    我希望它会工作。

    【讨论】:

    • 你写的东西是正确的,但是它们对于java开发来说就像使用';'以及设置 LayoutManger 的语法,我在 kotlin 中执行此操作。
    猜你喜欢
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 2021-08-29
    • 2021-01-11
    • 2020-07-12
    • 1970-01-01
    相关资源
    最近更新 更多