Firebase Android Studio 中的无效文档参考

Posted

技术标签:

【中文标题】Firebase Android Studio 中的无效文档参考【英文标题】:Invalid Document Reference in Firebase Android studio 【发布时间】:2022-01-15 17:48:18 【问题描述】:

我一直在在线关注一个电子商务应用项目的项目,但我已经震惊了几天。每当用户单击产品仪表板时,应用程序就会崩溃,并且在调试控制台上会显示无效的文档引用。

我认为这个错误是因为我传递了一个空的文档字段,但我不知道如何解决它。下面是调试控制台引用的第 254 行

 fun getProductDetailsFromFirestore(activity: ProductDetailsActivity, product_id: String) 
    mFireStore.collection(Constants.PRODUCTS)
        .document(product_id)
            .get()
            .addOnSuccessListener  documents ->
                Log.i(activity.javaClass.simpleName, documents.toString())

                // converting to object
                val product = documents.toObject(Product::class.java)

                if (product != null) 
                    activity.productDetailsRetrievedSuccessfully(product)
                

            

            .addOnFailureListener  error ->
                activity.dismissProgressDialogue()
                Log.e(activity.javaClass.simpleName, "Error updating product", error)
            

调试控制台引用的 ProductDetailsActivity 如下

 private var mProductID: String = ""


 private fun getProductDetails()
    showProgressDialogue("Please wait")
    FirestoreClass().getProductDetailsFromFirestore(this, mProductID)

【问题讨论】:

不要像现在这样为mProductID 传递一个空字符串,而是要传递用户单击的产品ID。如果您不知道该怎么做,这可能是一个不错的起点:google.com/search?q=androd+detect+what+item+a+user+clicked+on 【参考方案1】:

问题不是因为空字符串而是意图。我注释掉

startActivity(Intent(context, ProductDetailsActivity::class.java) .putExtra(Constants.EDIT_PRODUCT_ID, product.product_id))

来自我的 DashboardFragment Activity 并将其更改为

 val intent = Intent(context, ProductDetailsActivity::class.java)
    intent.putExtra(Constants. EXTRA_PRODUCT_ID, product.product_id)
    startActivity(intent)
    

令人惊讶的是,它解决了我的问题。下面是我的 DashboardFragment Activity 的代码

 class DashboardFragment : BaseFragment() 

    lateinit var rv_my_dashboard_item: RecyclerView
    lateinit var tv_dashboard_no_found: TextView

  //  private lateinit var dashboardViewModel: DashboardViewModel
    private var _binding: FragmentDashboardBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    // call when the fragment is created
    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        // to enable menu option in a fragment
        setHasOptionsMenu(true)
    

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) 
        super.onViewCreated(view, savedInstanceState)
        rv_my_dashboard_item = view.findViewById(R.id.rv_dashboard_items)
        tv_dashboard_no_found= view.findViewById(R.id.tv_no_dashboard_items_found)
    

    // call whenever the view is created
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? 
        //  dashboardViewModel = ViewModelProvider(this).get(DashboardViewModel::class.java)

        _binding = FragmentDashboardBinding.inflate(inflater, container, false)

        return binding.root
    

    override fun onDestroyView() 
        super.onDestroyView()
        _binding = null
    

    override fun onResume() 
        super.onResume()
        getDashboardItemListFromFirestore()
    

    // in order to create the sub menu or menu in the fragment
    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) 
        inflater.inflate(R.menu.dashboard_menu, menu)
        super.onCreateOptionsMenu(menu, inflater)
    

    override fun onOptionsItemSelected(item: MenuItem): Boolean 
        when(item.itemId)
            R.id.action_setting->
                startActivity(Intent(activity, SettingActivity::class.java))
                return true
            
        
        return super.onOptionsItemSelected(item)
    

    fun successfullyUploadedDashboardItemListFromFirestore(dashBoardItemList: ArrayList<Product>) 
        dismissProgressDialogue()
//        for (i in dashBoardItemList)
//            Log.i("Item List Title", i.product_title)
//        

        if (dashBoardItemList.size > 0) 
            rv_my_dashboard_item.visibility = View.VISIBLE
            tv_dashboard_no_found.visibility = View.GONE
            rv_my_dashboard_item.layoutManager = GridLayoutManager(activity, 2)
            rv_my_dashboard_item.setHasFixedSize(true)
            val dashboardItemListAdapter =
                DashboardItemListAdapter(requireActivity(), dashBoardItemList)
            rv_my_dashboard_item.adapter = dashboardItemListAdapter

            dashboardItemListAdapter.setOnclickListener(object :
                DashboardItemListAdapter.MyInterfaceOnclickListener 
                override fun onClick(position: Int, product: Product) 

    //startActivity(Intent(context, ProductDetailsActivity::class.java)
    //.putExtra(Constants.EDIT_PRODUCT_ID, product.product_id))
                      
      val intent = Intent(context, ProductDetailsActivity::class.java)
                        intent.putExtra(Constants.EXTRA_PRODUCT_ID, product.product_id)
                        startActivity(intent)
                    
            )
         else 
            rv_my_dashboard_item.visibility = View.GONE
            tv_dashboard_no_found.visibility = View.VISIBLE
        
    

    private fun getDashboardItemListFromFirestore() 
        showProgressDialogue("Please Wait")
        FirestoreClass().getDashboardItemList(this)
    
 // end of the fragment class

【讨论】:

以上是关于Firebase Android Studio 中的无效文档参考的主要内容,如果未能解决你的问题,请参考以下文章