NullPointerException:createProgressDialog
Posted
技术标签:
【中文标题】NullPointerException:createProgressDialog【英文标题】:NullPointerException: createProgressDialog 【发布时间】:2021-01-27 15:47:48 【问题描述】:从 Crashlytics 获取 android version: 10
的下一个错误:
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.app.Dialog.<init>(Dialog.java:196)
at android.app.ColorBaseAlertDialog.<init>(ColorBaseAlertDialog.java:33)
at android.app.AlertDialog.<init>(AlertDialog.java:208)
at android.app.AlertDialog.<init>(AlertDialog.java:204)
at android.app.ProgressDialog.<init>(ProgressDialog.java:112)
at com.sau.authenticator.widget.fragment.BaseFragment.createProgressDialog(BaseFragment.java:46)
at com.sau.authenticator.widget.fragment.BaseFragment.showLoadProgress(BaseFragment.java:41)
at com.sau.authenticator.widget.fragment.WebViewFragment$webViewClient$1.onPageStarted(WebViewFragment.java:69)
at i6.c(i6.java:2)
at Hn.handleMessage(Hn.java:145)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:228)
at android.app.ActivityThread.main(ActivityThread.java:7782)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:981)
在我的函数createProgressDialog
中,它在BaseFragment中被调用:
abstract class BaseFragment : Fragment()
private var progressDialog: ProgressDialog? = null
val activityComponents: ActivityComponentsContract?
get() = activity?.activityComponentsContract
override fun onDestroy()
progressDialog?.dismiss()
progressDialog = null
super.onDestroy()
protected fun showLoadProgress()
if (progressDialog == null) progressDialog = createProgressDialog()
progressDialog?.show()
private fun createProgressDialog(): ProgressDialog?
val dialog = ProgressDialog(activity, R.style.ProgressDialogTheme)
dialog.setCancelable(false)
dialog.setProgressStyle(android.R.style.Widget_ProgressBar_Large)
return dialog
protected fun dismissLoadProgress()
progressDialog?.dismiss()
这是我的 WebViewFragment 的代码:
class WebViewFragment : BaseFragment()
private var url = ""
private var title = ""
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
arguments?.let
url = it.getString(KEY_URL, "")
title = it.getString(KEY_TITLE, "")
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View
activityComponents?.updateAppbar(
title = title,
backActionImageResId = R.drawable.ic_appbar_action_back
)
return inflater.inflate(R.layout.fragment_web_view, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
super.onViewCreated(view, savedInstanceState)
customWebView?.webViewClient = webViewClient
customWebView?.loadUrl(url)
private val webViewClient = object : WebViewClient()
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?)
super.onPageStarted(view, url, favicon)
showLoadProgress()
override fun onPageFinished(view: WebView?, url: String?)
super.onPageFinished(view, url)
dismissLoadProgress()
companion object
const val KEY_URL = "KEY_URL"
fun newBundle(url: String = "", title: String): Bundle
return Bundle().apply
putString(KEY_URL, url)
putString(KEY_TITLE, title)
【问题讨论】:
【参考方案1】:我对@987654321@ 不太熟悉,但似乎当onPageStarted()
被称为您的用户时,您的用户已经离开(或旋转了他的设备)。因此,您的Fragment
的onDestroyView()
可能已经被调用,但您没有通知您的WebView
开始加载您的网址。这就是为什么 activity
是 null
而你得到了例外。
因此,在您的 WebViewFragment
的生命周期回调中,请确保您也通知您的 WebView
:
override fun onResume()
super.onResume()
customWebView?.onResume()
override fun onPause()
super.onPause()
customWebView?.onPause()
override fun onDestroyView()
super.onDestroyView()
customWebView?.stopLoading() // this should prevent onPageStarted() from being called
【讨论】:
我不太明白应该在我的片段中使用 WebView 进行哪些更改。我在问题中添加了片段代码。 我用代码示例更新了答案。对stopLoading()
的调用应防止调用onPageStarted()
并因此引发异常。
别忘了在onViewCreated()
中初始化customWebView
,我在你的代码中没有看到
谢谢你的答案,但你如何看待初始化?现在我有`customWebView?.webViewClient = webViewClient customWebView?.loadUrl(url)`
好吧,我希望在那里看到customWebView = ...
。我也找不到你在哪里声明customWebView
。【参考方案2】:
您的 Fragment 为 DestroyView 时似乎发生了此错误。我对此并不完全确定。您可以通过加载一个大型网页来重现,然后导航到另一个片段,以便执行 WebViewFragment / onViewDestroyed。在 onPageStarted 和 onPageFinished 中打印日志。如果您在销毁视图时看到它们被调用,这正是原因。此时您只需要检查生命周期状态即可显示或隐藏进度。
【讨论】:
以上是关于NullPointerException:createProgressDialog的主要内容,如果未能解决你的问题,请参考以下文章
No implementation found for long com.baidu.platform.comjni.map.commonmemcache.JNICommonMemCache.Crea
[Android] Android Error: Suspicious namespace and prefix combination [NamespaceTypo] when I try crea
Exception in thread "main" java.lang.NoSuchMethodError: scala.runtime.VolatileByteRef.crea