android - 深层链接意图返回未解析的引用
问题描述
我正在尝试获取具有深度链接的类别中的帖子列表,但它返回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
任何想法?
解决方案
您需要在活动类中处理意图数据,然后您需要将意图传递给片段构造函数, 请参阅下面的代码。
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)
}}
我希望这将有所帮助。
推荐阅读
- python - JSONDecodeError:期望值:第 1 行第 1 列(字符 0
- c# - Azure Functions 不断返回“无法识别提供给包的凭据”
- html - 我正在尝试获得响应式标题 + 带有图像的列
- c++ - 将类对象添加到存在于不同类中的向量
- microsoft-graph-sdks - 更改密码请求正在注销敏感信息
- python - Exception...NoReverseMatch ...Reverse for 'venue' with arguments '('',)' not found。尝试了 1 种模式:['venue/(?P
[0-9]+)$'] - python - 如何更快地将多个 csv 文件转换为数组?
- django - 如何允许反转到 urls.py 中的两个匹配项之一?
- tkinter - 自动调整大小的 Tkinter 条目需要初始宽度
- sass - 我是否需要在此 SCSS 代码中使用“&”?有或没有它,我得到相同的输出