Fragment 的 onCreateView() 没有被调用
我正在创建我的片段的一个实例,然后尝试调用它的方法之一,但需要在 onCreateView() 中实例化一个变量,然后才能调用该方法而不抛出 NullPointerException。
这是我的片段的一部分:
class UpdatesFragment : androidx.fragment.app.Fragment(){
var progressBar: ProgressBar? = null
private var instance: UpdatesFragment? = null
private var application: CustomApplication? = null
var bluetoothManager2: BluetoothManager? = null
private lateinit var li: LayoutInflater
private var deviceContainer: ViewFlipper? = null
private var devicePrep: View? = null
private var progressLayout: View? = null
private var progressText: TextView? = null
var progressBarText: TextView? = null
var progressChunk: TextView? = null
private var scroll: ScrollView? = null
private var logWindow: TextView? = null
private var currentView = 0
private var disconnected = false
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
View? {
instance = this
application = activity?.application as CustomApplication
if (bluetoothManager2 == null) {
bluetoothManager2 = SuotaManager() // THIS LINE NEEDS TO BE CALLED BEFORE MY METHOD IS CALLED.
> }
bluetoothManager2!!.setContext(this)
bluetoothManager2!!.setDevice(application!!.device)
progressText = progressLayout!!.findViewById(R.id.progress_text)
progressChunk = progressLayout!!.findViewById(R.id.progress_chunk)
progressBar = progressLayout!!.findViewById(R.id.progress_bar)
progressBarText = progressLayout!!.findViewById(R.id.progress_bar_text)
scroll = progressLayout!!.findViewById<View>(R.id.logScroll) as ScrollView
logWindow = progressLayout!!.findViewById<View>(R.id.logWindow) as TextView
logWindow!!.setText(null, TextView.BufferType.EDITABLE)
progressBar!!.progress = 0
progressBar!!.max = 100
initDevicePrepScreen()
val fragmentView = inflater.inflate(R.layout.activity_fragment, container, false)
li = LayoutInflater.from(activity)
deviceContainer = fragmentView.findViewById(R.id.deviceLayoutContainer) as ViewFlipper
devicePrep = inflater.inflate(R.layout.device_prep, deviceContainer, true)
progressLayout = inflater.inflate(R.layout.progress, deviceContainer, true)
return fragmentView
> }
这是我从活动中调用它的位置:
bluetoothGattReceiver =
object : BluetoothGattReceiver() {
override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
if (updatesFragment == null) {
updatesFragment = UpdatesFragment()
}
updatesFragment!!.processStep(intent)
如何在运行 updatesFragment!!.processStep(intent) 之前调用 onCreateView()?
这是堆栈跟踪:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.unobatteries.unobmsapp, PID: 31486
java.lang.NullPointerException
at com.unobatteries.unobmsapp.fragments.UpdatesFragment.processStep(UpdatesFragment.kt:85)
at com.unobatteries.unobmsapp.activities.UpdatesActivity$onResume$2.onReceive(UpdatesActivity.kt:144)
at androidx.localbroadcastmanager.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:313)
at androidx.localbroadcastmanager.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:121)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8653)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
fun processStep(intent: Intent?) {
bluetoothManager2!!.processStep(intent) // Line 85
}
I am creating an instance of my fragment and then trying to call one of it's methods but a variable needs to be instantiated in onCreateView() before the method can be called without throwing a NullPointerException.
Here is part of my fragment:
class UpdatesFragment : androidx.fragment.app.Fragment(){
var progressBar: ProgressBar? = null
private var instance: UpdatesFragment? = null
private var application: CustomApplication? = null
var bluetoothManager2: BluetoothManager? = null
private lateinit var li: LayoutInflater
private var deviceContainer: ViewFlipper? = null
private var devicePrep: View? = null
private var progressLayout: View? = null
private var progressText: TextView? = null
var progressBarText: TextView? = null
var progressChunk: TextView? = null
private var scroll: ScrollView? = null
private var logWindow: TextView? = null
private var currentView = 0
private var disconnected = false
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
View? {
instance = this
application = activity?.application as CustomApplication
if (bluetoothManager2 == null) {
bluetoothManager2 = SuotaManager() // THIS LINE NEEDS TO BE CALLED BEFORE MY METHOD IS CALLED.
> }
bluetoothManager2!!.setContext(this)
bluetoothManager2!!.setDevice(application!!.device)
progressText = progressLayout!!.findViewById(R.id.progress_text)
progressChunk = progressLayout!!.findViewById(R.id.progress_chunk)
progressBar = progressLayout!!.findViewById(R.id.progress_bar)
progressBarText = progressLayout!!.findViewById(R.id.progress_bar_text)
scroll = progressLayout!!.findViewById<View>(R.id.logScroll) as ScrollView
logWindow = progressLayout!!.findViewById<View>(R.id.logWindow) as TextView
logWindow!!.setText(null, TextView.BufferType.EDITABLE)
progressBar!!.progress = 0
progressBar!!.max = 100
initDevicePrepScreen()
val fragmentView = inflater.inflate(R.layout.activity_fragment, container, false)
li = LayoutInflater.from(activity)
deviceContainer = fragmentView.findViewById(R.id.deviceLayoutContainer) as ViewFlipper
devicePrep = inflater.inflate(R.layout.device_prep, deviceContainer, true)
progressLayout = inflater.inflate(R.layout.progress, deviceContainer, true)
return fragmentView
> }
And here is where I call it from the activity:
bluetoothGattReceiver =
object : BluetoothGattReceiver() {
override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
if (updatesFragment == null) {
updatesFragment = UpdatesFragment()
}
updatesFragment!!.processStep(intent)
How can I get onCreateView() to be called before I run updatesFragment!!.processStep(intent)
?
Here is the stack trace:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.unobatteries.unobmsapp, PID: 31486
java.lang.NullPointerException
at com.unobatteries.unobmsapp.fragments.UpdatesFragment.processStep(UpdatesFragment.kt:85)
at com.unobatteries.unobmsapp.activities.UpdatesActivity$onResume$2.onReceive(UpdatesActivity.kt:144)
at androidx.localbroadcastmanager.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:313)
at androidx.localbroadcastmanager.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:121)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8653)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
fun processStep(intent: Intent?) {
bluetoothManager2!!.processStep(intent) // Line 85
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:我意识到我需要调用此函数而不显示片段,同时仍然访问它引用的 bluetoothManager 类。有人向我指出,我不需要片段的实例来访问 bluetoothManager 中的函数,因为片段主要用于使用 ui 组件。
由于BluetoothManager类是抽象的,在我的活动中,我声明了一个SuotaManager类的实例,它扩展了BluetoothManager并在该实例上调用了processStep(),这解决了问题!
UPDATE: I realized that I needed to call this function without displaying the fragment while still accessing the bluetoothManager class that it references. It was pointed out to me that I didn't need an instance of my fragment to access the function in bluetoothManager because fragments are primarily for working with ui components.
Since BluetoothManager class is abstract, in my activity, I declared an instance of SuotaManager class which extends BluetoothManager and called processStep() on that instance and that fixed the problem!