首页 > 解决方案 > 深层链接意图返回未解析的引用

问题描述

我正在尝试获取具有深度链接的类别中的帖子列表,但它返回Unresolved reference

截图

Documents

一

my code

二

Errors

三

代码

class CategoryLinkDetails : Fragment() {

    private lateinit var mInterstitialAd: InterstitialAd
    private lateinit var homeViewModel: HomeViewModel
    private var recyclerView: RecyclerView? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        super.onCreate(savedInstanceState)
        homeViewModel = ViewModelProviders.of(this).get(HomeViewModel::class.java)

        val root = inflater.inflate(R.layout.categorylink_details, container, false)
        recyclerView = root.findViewById(R.id.categorylinkRecycle)

        handleIntent(intent)
        return root
    }

// api code
    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        handleIntent(intent)
    }

    private fun handleIntent(intent: Intent) {
        val appLinkAction = intent.action
        val appLinkData: Uri? = intent.data

        if (Intent.ACTION_VIEW == appLinkAction) {
            appLinkData?.lastPathSegment?.also { recipeId ->
                Uri.parse("content://com.my.app/categories/")
                    .buildUpon()
                    .appendPath(recipeId)
                    .build().also { appData ->
                        // Instantiate the RequestQueue.
                        val queue = Volley.newRequestQueue(activity)
                        val url = "https://example.com/v1/categories/$recipeId"

                        // Request a string response from the provided URL.
                        val stringRequest = StringRequest(
                            Request.Method.GET, url,
                            Response.Listener<String> { response ->
                                val jsonArray = JSONArray(response)
                                val list: ArrayList<MyCategoryLink> = ArrayList()
                                for (i in 0 until jsonArray.length()) {
                                    val jsonObject = jsonArray.getJSONObject(i)
                                    list.add(parseData(jsonObject))
                                }
                                // here you will have the complete list of data in your "list" variable
                                categoriesRecycle.layoutManager = LinearLayoutManager(activity)
                                Log.d("my list", list.toString())
                                categoriesRecycle.adapter = MyCategoryListAdapter(list)
                            },
                            Response.ErrorListener { error ->
                                //displaying the error in toast if occurrs
                                Toast.makeText(activity, error.message, Toast.LENGTH_SHORT)
                                    .show()
                            })
                        // Add the request to the RequestQueue.
                        queue.add(stringRequest)
                    }
            }
        }
    }

    private fun parseData(jsonObject: JSONObject): MyCategoryLink {
        var listingObject = MyCategoryLink(
            jsonObject.getString("name"),
            jsonObject.getString("slug"),
            HtmlCompat.fromHtml(jsonObject.getString("body"), HtmlCompat.FROM_HTML_MODE_COMPACT),
            jsonObject.getString("image")
        )
        return listingObject
    }
}

笔记:

我的代码和文档之间的唯一区别是我使用的Fragment是因为我需要显示帖子列表但在他们使用的文档中Activity

任何想法?

标签: androidandroid-studiokotlindeep-linking

解决方案


您需要在活动类中处理意图数据,然后您需要将意图传递给片段构造函数, 请参阅下面的代码。

class TestActivity :AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    handleIntent(intent)
}

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
    handleIntent(intent)
}

private fun handleIntent(intent: Intent?) {
    // now from here you can add your fragment here
    var categoryDetails = CategoryDetailsFragment(intent)
    var ft = supportFragmentManager.beginTransaction()
    ft.add(R.id.container,categoryDetails)
}}

我希望这将有所帮助。


推荐阅读