我正在创建片段的一个实例,然后尝试调用它的一个方法,但需要在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
}
UPDATE:我意识到我需要在不显示片段的情况下调用这个函数,同时仍然访问它引用的bluetoothManager类。有人向我指出,我不需要我的片段的实例来访问bluetoothManager中的函数,因为片段主要用于处理ui组件。
由于BluetoothManager类是抽象的,在我的活动中,我声明了SuotaManager类的一个实例,该实例扩展了BluetoothManager并在该实例上调用processStep((,从而解决了问题!