英文:
How can I observe Activity Live data class in Fragment Kotlin
问题
这是您提供的代码片段,以下是翻译的部分:
我一直在尝试在一个片段中观察这段代码,但我无法理解。如果我在普通的活动中使用它,它可以工作,但在片段中会出现错误。
class HomeFragment : Fragment() {
private lateinit var checkNetworkConnection: CheckNetworkConnection
private lateinit var internetLayout: RelativeLayout
private lateinit var noInternetLayout: RelativeLayout
private lateinit var tryAgainButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// 为此片段充气布局
return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
internetLayout = view.findViewById(R.id.internetLayout)
noInternetLayout = view.findViewById(R.id.noInternetLayout)
tryAgainButton = view.findViewById(R.id.try_again_button)
tryAgainButton.setOnClickListener {
callNetworkConnection()
}
callNetworkConnection()
val url = ""
val web = view.findViewById<View>(R.id.webView) as WebView
web.settings.javaScriptEnabled = true
web.settings.loadWithOverviewMode = true
web.settings.useWideViewPort = true
web.settings.builtInZoomControls = true
web.settings.pluginState = WebSettings.PluginState.ON
web.webViewClient = WebViewClient()
web.loadUrl(url)
}
private fun callNetworkConnection() {
checkNetworkConnection = CheckNetworkConnection()
checkNetworkConnection.observe(this,{ isConnected ->
if (isConnected){
internetLayout.visibility = View.VISIBLE
noInternetLayout.visibility = View.GONE
} else {
noInternetLayout.visibility = View.VISIBLE
internetLayout.visibility = View.GONE
}
})
}
}
这是您提到的错误信息:
以下函数都无法使用提供的参数调用:com.oreal.ng.CheckNetworkConnection中定义的公共构造函数(application:Application)com.oreal.ng.CheckNetworkConnection中定义的公共构造函数(connectivityManager:ConnectivityManager)
请帮我看看。
如果您需要更多帮助来解决这个错误,请提供更多上下文或具体问题的信息。
英文:
I have been trying to observe this code in a fragment but I am not getting it. If I use it on a normal activity it works but on fragment it brings errors.
class HomeFragment : Fragment() {
private lateinit var checkNetworkConnection: CheckNetworkConnection
private lateinit var internetLayout: RelativeLayout
private lateinit var noInternetLayout: RelativeLayout
private lateinit var tryAgainButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
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)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
internetLayout = view.findViewById(R.id.internetLayout)
noInternetLayout = view.findViewById(R.id.noInternetLayout)
tryAgainButton = view.findViewById(R.id.try_again_button)
tryAgainButton.setOnClickListener {
callNetworkConnection()
}
callNetworkConnection()
val url = ""
val web = view.findViewById<View>(R.id.webView) as WebView
web.settings.javaScriptEnabled = true
web.settings.loadWithOverviewMode = true
web.settings.useWideViewPort = true
web.settings.builtInZoomControls = true
web.settings.pluginState = WebSettings.PluginState.ON
web.webViewClient = WebViewClient()
web.loadUrl(url)
}
private fun callNetworkConnection() {
checkNetworkConnection = CheckNetworkConnection()
checkNetworkConnection.observe(this,{ isConnected ->
if (isConnected){
internetLayout.visibility = View.VISIBLE
noInternetLayout.visibility = View.GONE
} else {
noInternetLayout.visibility = View.VISIBLE
internetLayout.visibility = View.GONE
}
})
}
}
This the error I get:
> None of the following functions can be called with the arguments
> supplied: public constructor CheckNetworkConnection(application:
> Application) defined in com.oreal.ng.CheckNetworkConnection public
> constructor CheckNetworkConnection(connectivityManager:
> ConnectivityManager) defined in com.oreal.ng.CheckNetworkConnection
Please help me please
答案1
得分: 1
使用viewLifecycleOwner
而不是this
试试看。
英文:
Give viewLifecycleOwner
a try instead of this
.
答案2
得分: 0
使用requireContext()
代替this
。
英文:
Instead of this
try using requireContext()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论