Get next error from Crashlytics for 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)
In my function createProgressDialog, which is called in 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()
}
}
Here is code of my 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)
}
}
}
}
I'm not too familiar with WebView, but it seems like by the time onPageStarted() is called your user already navigated away (or rotated his device). Hence your Fragment's onDestroyView() is likely called already, but you are not notifying your WebView that starts loading your url. That's why activity is null and you get the exception.
So on your WebViewFragment's lifecycle callbacks make sure you notify your WebView too:
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
}
It seems that this error happened when your Fragment was DestroyView. I am not entirely sure about this. You can reproduce by load a large web page, then navigate to another fragment so that WebViewFragment / onViewDestroyed is executed. Print logs in onPageStarted and onPageFinished. If you see them being called when you destroy the view, this is exactly the cause. At this point you just need to check the lifecycle state to show or hide progress.
Related
I have a full screen DialogFragment that shows a WebView in my app:
class WebViewFragment(private val webTitle: String, private var webUrl: String) : DialogFragment() {
private lateinit var toolbar: Toolbar
private lateinit var progressBar: LinearProgressIndicator
private var webView: WebView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL, R.style.AppTheme_FullScreenDialog)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_full_screen_dialog, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
toolbar = view.findViewById(R.id.full_screen_dialog_toolbar)
progressBar = view.findViewById(R.id.progress_bar)
webView = view.findViewById(R.id.web_view)
toolbar.also {
it.setNavigationOnClickListener {
clearWebView()
clearCookiesAndWebStorage()
dismiss()
}
it.title = webTitle
}
webView?.also {
it.webChromeClient = WebChromeClient()
it.setLayerType(View.LAYER_TYPE_HARDWARE, null)
it.scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY
it.settings.apply {
allowFileAccess = false
allowContentAccess = false
javaScriptEnabled = true
domStorageEnabled = true
mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
cacheMode = WebSettings.LOAD_NO_CACHE
useWideViewPort = true
loadWithOverviewMode = true
setSupportZoom(true)
builtInZoomControls = true
displayZoomControls = false
loadsImagesAutomatically = true
}
}
webView?.webViewClient = object : WebViewClient() {
override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
progressBar.visibility = View.VISIBLE
}
override fun onPageCommitVisible(view: WebView, url: String) {
super.onPageCommitVisible(view, url)
progressBar.visibility = View.INVISIBLE
}
}
webView?.loadUrl(webUrl)
}
fun display(fragmentManager: FragmentManager): WebViewFragment {
val fullScreenDialog = WebViewFragment(webTitle, webUrl)
fullScreenDialog.show(fragmentManager, FULL_SCREEN_DIALOG_TAG)
return fullScreenDialog
}
private fun clearWebView() {
webView?.apply {
clearCache(true)
clearHistory()
clearFormData()
clearSslPreferences()
invalidate()
destroy()
}
webView = null
}
private fun clearCookiesAndWebStorage() {
CookieManager.getInstance().removeAllCookies(null)
CookieManager.getInstance().flush()
WebStorage.getInstance().deleteAllData()
}
companion object {
private const val FULL_SCREEN_DIALOG_TAG = "FULL_SCREEN_DIALOG"
}
}
When the user opens the WebView and then clicks on the X button on the toolbar to close it, it clears the cache that the WebView stored. However, let's say the user closes the app while the WebView screen was showing. It does not clear the cache or data stored from WebView. I've tried calling clearWebView() and clearCookiesAndWebStorage() in onDestroy(), but that doesn't seem to work. I've also noticed the data for the application goes up by a few megabytes when the user opens a WebView for the first time. Originally, the application data is only few kilobytes, but WebView increases it by a lot and I can't seem to figure out how to clear it completely after the user is done with the WebView.
How do I always ensure the data and cache that was used for the WebView to display a website is completely cleared whether the user closes the WebView by using the toolbar or by closing the entire app?
MyActivity is...
private var preso: ClientResultPresentationFragment? = null
private var presoHelper: PresentationHelper? = null
private val presoListener = object: PresentationHelper.Listener{
override fun initSecondDisplay(display: Display?) {
Log.d("preso", "initSecondDisplay()")
preso = MytPresentationFragment.newInstance(this#MyActivity, display)
preso!!.show(fragmentManager, PRESO) // PRESO is a static value.
}
override fun clearPreso(switchToInline: Boolean) {
if (preso != null) {
preso!!.dismiss()
preso = null
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_my)
// Must open second display at the same time.
onCreatePreso()
refinedResultData = intent.extras!!.getParcelable(RESULT_DATA)
Log.d("result", "${resultData.toString()}")
init(resultData!!)
}
private fun init(data: ResultData){
initView(data)
preso!!.syncData(data)
}
private initView(data: ResultData){
// TODO: initViews...
}
private fun onCreatePreso(){
presoHelper = PresentationHelper(this, presoListener)
presoHelper!!.enable()
}
override fun onResume() {
super.onResume()
presoHelper?.onResume()
}
override fun onPause() {
presoHelper?.onPause()
super.onPause()
}
My Presentation Fragment is...
var mView: View? = null
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Log.d("preso", "onCreateView()")
mView = LayoutInflater.from(context).inflate(R.layout.fragment_my, container, false)
return mView
}
fun syncData(data: ResultData){
Log.d("preso", "syncData()->${data.toString()}")
initView()
// return >>> here...
mView!!.tv_title.text = "${data.title}" // <<< crash here >>>
// TODO: set initial data
}
fun initView(){
// initViews...
}
companion object {
fun newInstance(context: Context?, display: Display?): MyPresentationFragment {
val frag = MyPresentationFragment()
frag.setDisplay(context, display)
return frag
}
}
And the log is...
"resultData.toString()"
initSecondDisplay()
"syncData()->${data.toString()}"
crash------------------
if I just uncomment return part in syncData() of Presentation Fragment(It will not access the views)
"resultData.toString()"
initSecondDisplay()
"syncData()->${data.toString()}"
onCreateView()
So, syncData() is called earlier than onCreateView(). What should I do? I moved preso!!.syncData() after preso!!.show(fragmentManager, PRESO) and it's the same.
Since FragmentManager operations is async, you should send a callback from your fragment to the activity in the fragment OnViewCreated or maybe OnResume and only then send the data from the activity to the fragment.
Or you can just use ViewModel and LiveData to provide your data to fragments
my webview show blank page
this is my Activity Class.
class AboutFragment : BaseGreenHCMFragment() {
override val layoutId = R.layout.fragment_about
override fun setupData() {}
override fun setupView() {
val mWebView: WebView? = view?.findViewById(R.id.webviewdaum) as WebView
mWebView?.loadUrl("greenhcm.com/")
val webSettings = mWebView?.getSettings()
webSettings?.setJavaScriptEnabled(true)
// Force links and redirects to open in the WebView instead of in a browser
//mWebView?.setWebViewClient(WebViewClient())
}
}
this is my xml & fragment
anyway check this code.
class MainActivity : AppCompatActivity() {
var mywebview: WebView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mywebview = findViewById<WebView>(R.id.webview)
mywebview!!.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
view?.loadUrl(url)
return true
}
}
mywebview!!.loadUrl("https://www.google.co.in/")
}
}
you need WebViewClient in your code.
My app has few activities and fragments. One of the activities is radio player. User can control player using a media notification (and a service). And it's working nice. But then I added to the radio activity a fragment with webview (site "chatovod"). And saw when I open this fragment (webview) my media notification is closing (but not service - music still playing). By the way other notifications (from the image activity) still showing.
class ChatovodFragment: Fragment() {
private lateinit var webView: WebView
companion object {
fun newInstance(url: String): ChatovodFragment {
val fragment = ChatovodFragment()
val bundle = Bundle()
bundle.putString(BUNDLE_CHATOVOD_URL, url)
fragment.arguments = bundle
return fragment
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activity?.onBackPressedDispatcher?.addCallback(this, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (webView.canGoBack()) {
webView.goBack()
} else {
activity?.supportFragmentManager?.popBackStack()
}
}
})
}
#SuppressLint("SetJavaScriptEnabled")
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_chatovod, container, false)
val url: String =
if (arguments != null) {
requireArguments().getString(BUNDLE_CHATOVOD_URL, "")
} else
""
webView = view.findViewById(R.id.webView)
webView.settings.javaScriptEnabled = true
webView.loadUrl(url)
webView.webViewClient = MyWebViewClient()
return view
}
class MyWebViewClient: WebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
view?.loadUrl(request?.url.toString())
return true
}
}
The service code is big. So if you need it I could load it to other place.
Sorry for my ignorance of English,but i hope you could understand what i am trying to anwser.
This question happened more than one place in my application.
In my application it is the audio focus make it.
Code:
systemService.abandonAudioFocus(new AudioManager.OnAudioFocusChangeListener() {
#Override
public void onAudioFocusChange(int focusChange) {
}
});
or other override method with it .
Do something in it.
Hope it helps you man.
I am using Kotlin Android Extension to access view directly by their id.
I have a progress bar which I access directly in fragment using id i.e progress_bar
<ProgressBar
android:id="#+id/progress_bar"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="15dp"
android:indeterminate="true"/>
In fragment, I am showing and hiding it with this code
progress_bar.visibility = if (visible) View.VISIBLE else View.GONE
It is working perfectly until I rotate the screen. After that, it throws the exception
java.lang.IllegalStateException: progress_bar must not be null.
The variable gets null on screen rotation. How to solve this problem?
Fragment code
class SingleAppFragment : Fragment() {
private lateinit var appName: String
companion object {
fun newInstance(appName: String = ""): SingleAppFragment {
val fragment = SingleAppFragment()
val args = Bundle()
args.putString(Constants.EXTRA_APP_NAME, appName)
fragment.arguments = args
return fragment
}
}
private var mListener: OnFragmentInteractionListener? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
appName = if (arguments != null && !arguments.getString(Constants.EXTRA_APP_NAME).isEmpty()) {
arguments.getString(Constants.EXTRA_APP_NAME)
} else {
Constants.APP_NAME_FACEBOOK
}
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater!!.inflate(R.layout.fragment_single_app, container, false)
}
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initView()
setEventListeners()
}
private fun initView() {
var canShowSnackBar = true
web_single_app.webViewClient = object : WebViewClient() {
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
showHideProgressBar(true)
canShowSnackBar = true
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
showHideProgressBar(false)
}
override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
web_single_app.stopLoading()
if (canShowSnackBar) {
mListener?.onErrorWebView()
canShowSnackBar = false
}
}
}
web_single_app.settings.javaScriptEnabled = true
web_single_app.loadUrl(Constants.APP_NAME_URL_MAP[appName])
}
private fun setEventListeners() {
back_web_control.setOnClickListener({
web_single_app.goBack()
})
}
fun showHideProgressBar(visible: Boolean) {
progress_bar_web_control.visibility = if (visible) View.VISIBLE else View.GONE
}
fun loadUrl(appName: String) {
web_single_app.loadUrl(Constants.APP_NAME_URL_MAP[appName])
}
override fun onAttach(context: Context?) {
super.onAttach(context)
if (context is OnFragmentInteractionListener) {
mListener = context
}
}
override fun onDetach() {
super.onDetach()
mListener = null
}
interface OnFragmentInteractionListener {
fun onErrorWebView()
}
}
Steps to reproduce:
Start Activity
Fragment get loaded
At Fragment load, I load an URL and show a progress bar
At loading the URL I rotate the phone and the progress bar variable gets null
In my case this bug happens from time to time. Of course, onViewCreated() is a good method to place your code in. But sometimes it's strangely not enough. And setRetainInstance(true) may help, may not. So sometimes this helps: access your Views with a view variable. You can even access them inside onCreateView(). You can use ?. for a guarantee that an application won't crash (of course, some views won't update in this case). If you wish to get context, use view.context.
In my case this bug reproduced only in Kotlin coroutines.
private fun showProgress(view: View) {
view.progressBar?.visibility = View.VISIBLE
}
private fun hideProgress(view: View) {
view.progressBar?.visibility = View.GONE
}
Then in code:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
showData(view)
}
private fun showData(view: View) {
showProgress(view)
adapter = SomeAdapter()
adapter.setItems(items)
val divider = SomeItemDecoration(view.context)
view.recycler_view?.run {
addItemDecoration(divider)
adapter = this#SomeFragment.adapter
layoutManager = LinearLayoutManager(view.context)
setHasFixedSize(true)
}
hideProgress(view)
}
In which method do you get the progress_bar by Id?
Please consider the fragment state lifecycle. Maybe you try to load it when the view is not ready yet.
Ensure your progress_bar variable is assigned only after the view is ready. For example in the
onViewCreated method.
See here the official Android lifecycle:
Update
As #CoolMind pointed out the diagram doesn't show the method onViewCreated.
The complete Android Activity/Fragment lifecycle can be found here:
Add retain intance true to the fragment so that it will not be destroyed when an orientation changes occurs
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
retainInstance=true
}
Also do a null check using safe call operator before accessing views
fun showHideProgressBar(visible: Boolean) {
progress_bar_web_control?.visibility = if (visible) View.VISIBLE else View.GONE
}