kotlin 中 "this" 的类型不匹配
Mismatched type on "this" in kotlin
我试图从 api 获取数据,但我 运行 在此行出错
val queue = Volley.newRequestQueue(this@MainActivity)
错误显示“类型不匹配:推断的类型是 Home 但上下文!是预期的”
我曾尝试将“this”更改为“this@MainActivity”,但这只会让 Main Activity 变红,当我将鼠标悬停在它上面时,它显示未解决的参考
我的全部代码如下,感谢您的提前帮助
package com.example.temperaturesensor
import android.app.DownloadManager
import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import org.json.JSONArray
import org.json.JSONTokener
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [Home.newInstance] factory method to
* create an instance of this fragment.
*/
class Home : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val queue = Volley.newRequestQueue(this@MainActivity)
val url = "https://io.adafruit.com/api/v2/alex9301/feeds/test-data/data" // Your URL
var urlStuff = ""
val stringRequest = StringRequest(Request.Method.GET, url,
{ response ->
// Display the first 500 characters of the response string.
urlStuff = response
val jsonArray = JSONTokener(urlStuff).nextValue() as JSONArray
for (i in 0 until jsonArray.length()) {
// ID
val id = jsonArray.getJSONObject(i).getString("value") // Get data
Log.i("Val: ", id)
}
},
{ Log.i("b", "That didn't work!") })
queue.add(stringRequest)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Home.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
Home().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
package com.example.temperaturesensor
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import org.json.JSONArray
import org.json.JSONTokener
class Home : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_home, container, false)
val queue = Volley.newRequestQueue(view.context)
val url = "https://io.adafruit.com/api/v2/alex9301/feeds/test-data/data" // Your URL
var urlStuff = ""
val stringRequest = StringRequest(Request.Method.GET, url,
{ response ->
// Display the first 500 characters of the response string.
urlStuff = response
val jsonArray = JSONTokener(urlStuff).nextValue() as JSONArray
for (i in 0 until jsonArray.length()) {
// ID
val id = jsonArray.getJSONObject(i).getString("value") // Get data
Log.i("Val: ", id)
}
},
{ Log.i("b", "That didn't work!") })
queue.add(stringRequest)
return view
}
}
使用 requireContext() 因为你在片段中而不是 Activity
我试图从 api 获取数据,但我 运行 在此行出错
val queue = Volley.newRequestQueue(this@MainActivity)
错误显示“类型不匹配:推断的类型是 Home 但上下文!是预期的”
我曾尝试将“this”更改为“this@MainActivity”,但这只会让 Main Activity 变红,当我将鼠标悬停在它上面时,它显示未解决的参考
我的全部代码如下,感谢您的提前帮助
package com.example.temperaturesensor
import android.app.DownloadManager
import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import org.json.JSONArray
import org.json.JSONTokener
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [Home.newInstance] factory method to
* create an instance of this fragment.
*/
class Home : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val queue = Volley.newRequestQueue(this@MainActivity)
val url = "https://io.adafruit.com/api/v2/alex9301/feeds/test-data/data" // Your URL
var urlStuff = ""
val stringRequest = StringRequest(Request.Method.GET, url,
{ response ->
// Display the first 500 characters of the response string.
urlStuff = response
val jsonArray = JSONTokener(urlStuff).nextValue() as JSONArray
for (i in 0 until jsonArray.length()) {
// ID
val id = jsonArray.getJSONObject(i).getString("value") // Get data
Log.i("Val: ", id)
}
},
{ Log.i("b", "That didn't work!") })
queue.add(stringRequest)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Home.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
Home().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
package com.example.temperaturesensor
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import org.json.JSONArray
import org.json.JSONTokener
class Home : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_home, container, false)
val queue = Volley.newRequestQueue(view.context)
val url = "https://io.adafruit.com/api/v2/alex9301/feeds/test-data/data" // Your URL
var urlStuff = ""
val stringRequest = StringRequest(Request.Method.GET, url,
{ response ->
// Display the first 500 characters of the response string.
urlStuff = response
val jsonArray = JSONTokener(urlStuff).nextValue() as JSONArray
for (i in 0 until jsonArray.length()) {
// ID
val id = jsonArray.getJSONObject(i).getString("value") // Get data
Log.i("Val: ", id)
}
},
{ Log.i("b", "That didn't work!") })
queue.add(stringRequest)
return view
}
}
使用 requireContext() 因为你在片段中而不是 Activity