18

Following my change describe in Force locale for Android flavor with resConfig I am facing a problem with webviews containing video. The issue is only on API21+ and really disappear when removing the call to applyOverrideConfiguration. Not so sure how to get around that.

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
  at com.android.webview.chromium.WebViewContentsClientAdapter.getDefaultVideoPoster(WebViewContentsClientAdapter.java:1172)
  at org.chromium.android_webview.DefaultVideoPosterRequestHandler$1.run(DefaultVideoPosterRequestHandler.java:39)
  at android.os.Handler.handleCallback(Handler.java:739)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:135)
  at android.app.ActivityThread.main(ActivityThread.java:5221)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

From what I could find on grepcode it would be while getting the ic_media_video_poster image.. I validated that this image is really in the sdk. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.1_r1/com/android/webview/chromium/WebViewContentsClientAdapter.java#WebViewContentsClientAdapter.getDefaultVideoPoster%28%29

public Bitmap More ...getDefaultVideoPoster() {
     TraceEvent.begin();
     Bitmap result = null;
     if (mWebChromeClient != null) {
         if (TRACE) Log.d(TAG, "getDefaultVideoPoster");
         result = mWebChromeClient.getDefaultVideoPoster();
     }
     if (result == null) {
         // The ic_media_video_poster icon is transparent so we need to draw it on a gray
         // background.
         Bitmap poster = BitmapFactory.decodeResource(
                 mWebView.getContext().getResources(),
                 R.drawable.ic_media_video_poster);
         result = Bitmap.createBitmap(poster.getWidth(), poster.getHeight(), poster.getConfig());
         result.eraseColor(Color.GRAY);
         Canvas canvas = new Canvas(result);
         canvas.drawBitmap(poster, 0f, 0f, null);
     }
     TraceEvent.end();
     return result;
 }

EDIT: After a few more test, I was able to isolate the crash in a testApp. It is available in the bugreport I created on Chromium https://code.google.com/p/chromium/issues/detail?id=521753

Any ideas? As anyone faced this problem already?

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
Eric Labelle
  • 1,987
  • 2
  • 15
  • 23
  • 1
    can you try passing a `Configuration` to `applyOverrideConfiguration` that you get from `Resources.getSystem().getConfiguration()`? – ahmedre Aug 21 '15 at 18:14
  • 6
    Following comment #8 on specified URL and implementing workaround it works! https://code.google.com/p/chromium/issues/detail?id=521753#c8 – Martin Edlman Jan 13 '16 at 13:03

2 Answers2

7

As @Martin Edlman commented, it should work with this workaround:

In Kotlin:

override fun getAssets(): AssetManager {
    return resources.assets
}

In Java:

@Override
public AssetManager getAssets() {
    return getResources().getAssets();
} 
Martin L. Jensen
  • 422
  • 4
  • 10
  • what a weird bug. I don't get why chromium made this bug in the first place. – rminaj May 20 '20 at 07:18
  • Wow it really fixed the issue! For me this issue occurred when I upgraded a relatively simple app to AndroidX - adding the above to the Activity class cleared up the crash. – TahoeWolverine Aug 25 '20 at 23:14
5

For future; You can try my own implementation. Add below code to your CustomChromeClient;

@Nullable
@Override
public Bitmap getDefaultVideoPoster() {
    if (super.getDefaultVideoPoster() == null) {
        return BitmapFactory.decodeResource(context.getResources(),
                R.drawable.ic_launcher);
    } else {
        return super.getDefaultVideoPoster();
    }
}
Ozan
  • 1,191
  • 2
  • 16
  • 31