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
}
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!