提问人:Vikalp Ranjan 提问时间:4/29/2023 最后编辑:Vikalp Ranjan 更新时间:4/29/2023 访问量:109
无法解决空指针异常
Can't resolve null pointer exception
问:
这是 playstore 给出的我的应用程序的堆栈跟踪。我找不到导致空指针异常的原因。 在我的设备上一切正常。
Exception java.lang.NullPointerException:
at in.vikalp.gridforart.ui.MainFragment$onCreateView$1.invoke (MainFragment.java:192)
at in.vikalp.gridforart.ui.MainFragment$onCreateView$1.invoke (MainFragment.java:192)
at in.vikalp.gridforart.ui.MainFragment$sam$androidx_lifecycle_Observer$0.onChanged (MainFragment.java)
at androidx.lifecycle.LiveData.considerNotify (LiveData.java)
at androidx.lifecycle.LiveData.dispatchingValue (LiveData.java:56)
at androidx.lifecycle.LiveData.setValue (LiveData.java)
at androidx.lifecycle.MutableLiveData.setValue (MutableLiveData.java)
at androidx.lifecycle.LiveDataScopeImpl$emit$2.invokeSuspend (LiveDataScopeImpl.java:44)
at androidx.lifecycle.LiveDataScopeImpl$emit$2.invoke (LiveDataScopeImpl.java)
at androidx.lifecycle.LiveDataScopeImpl$emit$2.invoke (LiveDataScopeImpl.java)
at kotlin.coroutines.jvm.internal.DebugProbesKt.j0 (DebugProbes.kt:5)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext (BuildersKt__Builders_common.kt:91)
at kotlinx.coroutines.BuildersKt.withContext (Builders.kt:91)
at androidx.lifecycle.LiveDataScopeImpl.emit (LiveDataScopeImpl.java)
at androidx.lifecycle.FlowLiveDataConversions$asLiveData$1$1.emit (FlowLiveDataConversions.java)
at in.vikalp.gridforart.datastoreRepo.DatastoreRepository$special$$inlined$map$1$2.emit (DatastoreRepository.java:270)
这是堆栈跟踪指向的主要片段,我也检查过这一点,但这对我来说也很清楚。我的手机是带有 android 31 sdk 的摩托罗拉 g32 。该错误出现在每种类型的设备上,因此它不是特定于 sdk 或特定于设备的。
``
@AndroidEntryPoint
class MainFragment : Fragment(), EasyPermissions.PermissionCallbacks {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
_binding = FragmentMainBinding.inflate(inflater, container, false)
binding.lifecycleOwner = this
binding.mainViewModel = mainViewModel
gridSettingsViewModels.readGridSettings.observe(viewLifecycleOwner) {
Log.e(TAG, "onCreateView: $it")
mainViewModel.setSettings(it)
if (oldImageUri.toString() != EMPTY_URI)
mainViewModel.createBitmap(oldImageUri!!)
}
mainViewModel.imageUriLiveData.observe(viewLifecycleOwner) {
Log.e(TAG, "onCreateView: $it")
oldImageUri = Uri.parse(it)
}
binding.gridButton.setOnClickListener {
findNavController().navigate(MainFragmentDirections.actionMainFragmentToGridBottomSheet())
}
binding.filterButton.setOnClickListener {
findNavController().navigate(MainFragmentDirections.actionMainFragmentToFilterDialogFragment())
}
binding.tapBarMenu.setOnClickListener {
binding.tapBarMenu.toggle()
}
binding.galleryButton.setOnClickListener {
getImageFromGallery()
}
binding.downloadButton.setOnClickListener {
mainViewModel.liveBitmapReady.observeOnce(viewLifecycleOwner){
savedImageBitmap(it)
}
}
return binding.root
}
}
private fun getImageFromGallery() {
if (PermissionUtils.hasCameraPermission(requireContext())) {
getImage.launch("image/*")
} else {
requestPermissions()
}
}
private fun requestPermissions() {
EasyPermissions.requestPermissions(
this,
"You need to accept storage permission to use this app",
REQUEST_CODE_CAMERA_PERMISSIONS,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
}
override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {
}
**-> this here is line 192**
override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
AppSettingsDialog.Builder(this).build().show()
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
}
``
答: 暂无答案
评论
.emit (DatastoreRepository.java:270)
LiveData.setValue (LiveData.java)
_Observer$0.onChanged (MainFragment.java)