Android Studio 3.6 在我的片段中:
class BluetoothPageFragment : Fragment() {
private lateinit var bluetoothPageViewModel: BluetoothPageViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val viewViewModelProvider = ViewModelProviders.of(activity!!) // one scope
bluetoothPageViewModel = viewViewModelProvider.get(BluetoothPageViewModel::class.java)
}
fun onClickButtonStartSearch(beaconManager: BeaconManager?) {
bluetoothPageViewModel.doClickButtonStartSearch(beaconManager)
}
我的视图模型:
class BluetoothPageViewModel(application: Application) : AndroidViewModel(application),
}
当我单击button时,成功调用onClickButtonStartSearch
和doClickButtonStartSearch
好。很好 但是,如果在单击之前我最小化了应用程序,更改了设备的语言,请回到我的应用程序并单击按钮,然后引发异常:
11-25 17:13:24.950 E/AndroidRuntime(10839): FATAL EXCEPTION: main
11-25 17:13:24.950 E/AndroidRuntime(10839): Process: com.myproject.android.debug, PID: 10839
11-25 17:13:24.950 E/AndroidRuntime(10839): java.lang.IllegalStateException: Could not execute method for android:onClick
11-25 17:13:24.950 E/AndroidRuntime(10839): at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
11-25 17:13:24.950 E/AndroidRuntime(10839): at android.view.View.performClick(View.java:5204)
11-25 17:13:24.950 E/AndroidRuntime(10839): at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:967)
11-25 17:13:24.950 E/AndroidRuntime(10839): at android.view.View$PerformClick.run(View.java:21153)
11-25 17:13:24.950 E/AndroidRuntime(10839): at android.os.Handler.handleCallback(Handler.java:739)
11-25 17:13:24.950 E/AndroidRuntime(10839): at android.os.Handler.dispatchMessage(Handler.java:95)
11-25 17:13:24.950 E/AndroidRuntime(10839): at android.os.Looper.loop(Looper.java:148)
11-25 17:13:24.950 E/AndroidRuntime(10839): at android.app.ActivityThread.main(ActivityThread.java:5417)
11-25 17:13:24.950 E/AndroidRuntime(10839): at java.lang.reflect.Method.invoke(Native Method)
11-25 17:13:24.950 E/AndroidRuntime(10839): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-25 17:13:24.950 E/AndroidRuntime(10839): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-25 17:13:24.950 E/AndroidRuntime(10839): Caused by: java.lang.reflect.InvocationTargetException
11-25 17:13:24.950 E/AndroidRuntime(10839): at java.lang.reflect.Method.invoke(Native Method)
11-25 17:13:24.950 E/AndroidRuntime(10839): at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
11-25 17:13:24.950 E/AndroidRuntime(10839): ... 10 more
11-25 17:13:24.950 E/AndroidRuntime(10839): Caused by: kotlin.UninitializedPropertyAccessException: lateinit property bluetoothPageViewModel has not been initialized
11-25 17:13:24.950 E/AndroidRuntime(10839): at com.myproject.android.ui.fragment.BluetoothPageFragment.onClickButtonStartSearch(BluetoothPageFragment.kt:237)
11-25 17:13:24.950 E/AndroidRuntime(10839): at com.myproject.android.ui.actviity.QRBluetoothSwipeActivity.onClickButtonStartSearch(QRBluetoothSwipeActivity.kt:213)
11-25 17:13:24.950 E/AndroidRuntime(10839): ... 12 more