![](/img/trans.png)
[英]How can I access MainActivity's objects or methods inside BroadcastReceiver?
[英]How can I hide and show views on a MainActivity from a BroadcastReceiver
所以我的BroadcastREceiver遇到错误...接收器正在运行并检测网络的变化..一旦添加可见性,它就会消失...
罐头化的地方是警告消息中可见性的变化
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val networkInfo = connectivityManager.activeNetworkInfo
if (networkInfo != null && networkInfo.detailedState == NetworkInfo.DetailedState.CONNECTED) {
Log.d("Network Connected","NC is connected")
MainActivity().warning_message.visibility = View.INVISIBLE
MainActivity().isConnected = true
} else if (networkInfo != null) {
val state = networkInfo.detailedState
Log.d("NetworkReceiver", state.name)
} else {
MainActivity().warning_message.visibility = View.VISIBLE
MainActivity().isConnected = false
Log.d("Network Connected","NC")
}
}
警告消息只是:
<FrameLayout
android:id="@+id/warning_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/top_toolbar">
在主要活动布局中。
我收到的错误是:进程:com.example.user.appname,PID:19972
java.lang.RuntimeException:接收com.example.user.appname.ConnectionReceiver@531d646中的广播意图{act = android.net.conn.CONNECTIVITY_CHANGE flg = 0x4000010(has extras)}时出错
没有关于原因的特定错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.