2016-10-13 10 views
2

にカスタムWebViewを作成して、正しい初期化が行われているかどうかを確認します。計装テストWebView

マイカスタムWebView

InstrumentationTest
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); 
     } 
    } 
} 

@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コンストラクタを呼び出すときに、私は次のエラーを取得することです:私は17行でInteractiveWebViewリンクをクリックすると

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) 

、それはコンストラクタに私を取りますそのクラス内でsuper(context);コールを指しています。

ActivityInstrumentationTestCase2のように拡張するなど、他の多くの方法を試してみましたが、それはヌルになるContextですが、それでも同じエラーが発生します。私は何度も問題を探検しましたが、私が見つけたものに基づいて問題を解決できないようです。

私は間違っていますか?

答えて

1

InteractiveWebViewをあざけるが、それはメインUIスレッド上でRunnableを実行中に建設されています確保することによってそれを解決:

getActivity().runOnUiThread(new Runnable() 
{ 
    @Override 
    public void run() 
    { 
     // Given 
     InteractiveWebView interactiveWebView = new InteractiveWebView(getActivity()); 
     .... 
     .... 
     .... 
     .... 
    } 
}