无法解决空指针异常

Can't resolve null pointer exception

提问人:Vikalp Ranjan 提问时间:4/29/2023 最后编辑:Vikalp Ranjan 更新时间:4/29/2023 访问量:109

问:

这是 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)
    }
``
Android kotlin null nullpointerexception 堆栈跟踪

评论

2赞 dominicoder 4/29/2023
您在代码段中标记的行是空行...您是否查看了堆栈跟踪?这很简单。 - 您的数据存储发出一个值(可能是 null),该值经过一些转换,并在 Your 中观察到的实时数据上设置,该数据显然没有为 null 值做好准备。因此,您需要在管道中的某个位置处理 null。如果 null 不可行,请调试应用.emit (DatastoreRepository.java:270)LiveData.setValue (LiveData.java)_Observer$0.onChanged (MainFragment.java)
1赞 Mikhail Guliaev 4/29/2023
你能提供onCreateView()方法的代码吗?
0赞 Vikalp Ranjan 4/29/2023
@dominicoder感谢您的反馈,但我仍然不明白是什么导致了问题
1赞 Mikhail Guliaev 4/29/2023
@VikalpRanjan您还能提供 ViewModel 的代码吗?
0赞 Vikalp Ranjan 5/15/2023
@MikhailGuliaev我终于解决了这个问题,谢谢。

答: 暂无答案