无法完成预定的刷新请求请求. ClientErrorCode:3个Android Kotlin

问题描述:

让我直截了当地指出logcat中的错误是:

Let me get straight to the point here the error in the logcat is:

Could not complete scheduled request to refresh entries. ClientErrorCode: 3

我已经测试了代码的Realm()部分,它获取了正确的数据.基本上,应用程序在加载该活动时就会崩溃.我现在想做的就是在每个单元格中张贴itemName.如果你们需要logcat,请说出来,我将其发布.还需要其他任何细节.

I have tested the Realm() part of the code and it fetched the right data. Basically, the app just crashes when it loads that Activity. All Im trying to do right now is post the itemName in each cell. If you guys need the logcat, just say so and I'll post it. Any other details needed too.

这是我的Activity的代码,其中每个单元格中只有一个ImageView和一个TextView的一个recyclerView.

This is the code for my Activity with a recyclerView with just an ImageView and a TextView in each cell.:

class EssentialsActivity : AppCompatActivity() {

    var category: String? = null
    val realmtypeFunctions = RealmTypeFunctions()
    var realmResults: RealmResults<ChattRItem>? = null
    var chattRItemList = mutableListOf<ChattRItem>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_essentials)

        //init realm
        Realm.init(this)

        category = "People"

        recyclerView_Essentials.setBackgroundColor(Color.CYAN)
        recyclerView_Essentials.layoutManager = GridLayoutManager(this, 3)
//        AsyncTask.execute {
            category?.let {
                loadFromRealm(it)
            }
//        }
        this.runOnUiThread {
            recyclerView_Essentials.adapter = EssentialsAdapter(chattRItemList)
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        val inflater = menuInflater
        inflater.inflate(R.menu.categories, menu )

        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        val intent: Intent?
        intent = Intent(this, AddItemActivity::class.java)
        intent.putExtra("category", category)
        startActivity(intent)


//        when (item?.itemId) {
//            R.id.essentials_menu_item -> {
//                intent = Intent(this, EssentialsActivity::class.java)
//                startActivity(intent)
//            }
//            R.id.addItem_menu_item -> {
//                intent = Intent(this, AddItemActivity::class.java)
//                startActivity(intent)
//            }
//            else -> return false
//        }

        return super.onOptionsItemSelected(item)
    }

    private fun loadFromRealm(category: String){
        val realm = Realm.getDefaultInstance()
        try {
            val query: RealmQuery<ChattRItem>? = realm.where(ChattRItem::class.java).equalTo("itemCategory", category)
            val result: RealmResults<ChattRItem>? = query?.findAll()
            result?.let {
                for (i in it) {
                    println(i.itemName)
                    chattRItemList.add(i)
                }
                println(chattRItemList.count())
            }

        } finally {
            realm.close()
        }
    }
}

class EssentialsAdapter(private val chattRItemList: List<ChattRItem>): RecyclerView.Adapter<CustomViewHolder>(){

    //realm class variable here to be displayed
    /* var essentials = array of realm essential item */
//    var essentialsActivity = EssentialsActivity()

    //number of items
    override fun getItemCount(): Int {
//        return 12 //return realm items count
        return this.chattRItemList.size
    }

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {

//        holder.itemView.textView_essentials_name.text = "Essentials Item"

        val chattRItem = chattRItemList.get(position)
//        holder.itemView.textView_essentials_name.text = chattRItem.itemName
        holder.bind(chattRItem)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder{
        // how do we create a cell view
        val layoutInflater = LayoutInflater.from(parent.context)
        val cellForRow = layoutInflater.inflate(R.layout.essentials_cells_layout, parent, false)
        return CustomViewHolder(view = cellForRow)
    }

}

class CustomViewHolder(view: View): RecyclerView.ViewHolder(view) {
    fun bind(chattRitem: ChattRItem) {
        itemView.textView_essentials_name.text = chattRitem.itemName
    }
}

所以基本上我已经弄清楚了.这不是LogCat的正确错误. Logcat在此上方的许多行中还有另一组错误.错误是结果列表是@Realm对象.我的recyclerView要求一个非RealmClass对象.因此,除了RealmClass之外,我不得不制作一个类似的对象.

So basically I figured it out. This was not the right error from LogCat. There was another set of errors from Logcat many lines above this. The error was the result list was a @Realm object. My recyclerView was asking for a non RealmClass object. So i had to make a similar object except not a RealmClass.

@RealmClass
open class ChattRItem: RealmModel {
    @PrimaryKey var itemId: String = ""
    var itemName: String = ""
    var itemCategory: String = ""
    var itemImageFileName: String = ""
    var itemAudioFileName: String = ""
}

class ChattRBoxItems(val itemId: String, val itemName: String, val itemCategory: String, val itemImageFileName: String, val itemAudioFileName: String)

然后我将结果映射到这个新类中,然后将其应用于我的recyclerView.

then I mapped the result into this new class then applied it to my recyclerView.