我只想在我的WebView
中构造一个自定义的InstrumentationTest
,然后检查初始化是否正确。
我的自定义WebView
:
public class InteractiveWebView extends WebView
{
public InteractiveWebView(final Context context)
{
super(context);
initialise(context);
}
public InteractiveWebView(final Context context, final AttributeSet attrs)
{
super(context, attrs);
initialise(context);
}
public InteractiveWebView(final Context context, final AttributeSet attrs, final int defStyleAttr)
{
super(context, attrs, defStyleAttr);
initialise(context);
}
private void initialise(final Context context)
{
if (!isInEditMode())
{
if (Build.VERSION.SDK_INT >= 19)
{
setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
else
{
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
setFocusable(true);
setFocusableInTouchMode(true);
requestFocus(View.FOCUS_DOWN);
WebSettings settings = getSettings();
settings.setJavaScriptEnabled(true);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setSupportMultipleWindows(false);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
settings.setSupportZoom(false);
settings.setUseWideViewPort(false);
String databasePath = context.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
settings.setDatabasePath(databasePath);
}
}
}
InstrumentationTest
:@RunWith(AndroidJUnit4.class)
@SmallTest
public class InteractiveWebViewTest
{
@Test
public void constructors()
{
Context baseContext = InstrumentationRegistry.getContext();
InteractiveWebView webView1 = new InteractiveWebView(baseContext);
InteractiveWebView webView2 = new InteractiveWebView(baseContext, null);
InteractiveWebView webView3 = new InteractiveWebView(baseContext, null, 1);
}
}
如你所见,我现在还没有断言任何事情。
我面临的问题是,当我调用第一个
WebView
构造函数时,会得到以下错误:java.lang.NullPointerException: Attempt to read from field 'android.os.MessageQueue android.os.Looper.mQueue' on a null object reference
at android.os.Handler.<init>(Handler.java:229)
at android.os.Handler.<init>(Handler.java:137)
at org.chromium.base.ThreadUtils.setUiThread(ThreadUtils.java:39)
at com.android.webview.chromium.WebViewChromiumFactoryProvider.ensureChromiumStartedLocked(WebViewChromiumFactoryProvider.java:197)
at com.android.webview.chromium.WebViewChromiumFactoryProvider.startYourEngines(WebViewChromiumFactoryProvider.java:294)
at com.android.webview.chromium.WebViewChromium.init(WebViewChromium.java:218)
at android.webkit.WebView.<init>(WebView.java:606)
at android.webkit.WebView.<init>(WebView.java:542)
at android.webkit.WebView.<init>(WebView.java:525)
at android.webkit.WebView.<init>(WebView.java:512)
at android.webkit.WebView.<init>(WebView.java:502)
at android.mobileconnect.gsma.com.library.view.InteractiveWebView.<init>(InteractiveWebView.java:17)
当我单击第17行的
InteractiveWebView
链接时,它会将我带到该类中的构造函数并指向super(context);
调用。我尝试了很多其他方法,比如将
InstrumentationTest
扩展为ActivityInstrumentationTestCase2
类型,以防Context
变成空值,但仍然会得到相同的错误。我在谷歌上搜索了好几次这个问题,但似乎无法根据我的发现来解决这个问题。你知道我错在哪里吗?
最佳答案
通过模拟InteractiveWebView
但确保它是在主ui线程上运行的runnable中构造的,解决了这个问题:
getActivity().runOnUiThread(new Runnable()
{
@Override
public void run()
{
// Given
InteractiveWebView interactiveWebView = new InteractiveWebView(getActivity());
....
....
....
....
}
}