致命异常:java.lang.NullPointerException 在 MainFragment

Fatal Exception: java.lang.NullPointerException at MainFragment

最近我发现我的应用程序在某些设备上崩溃了。这是崩溃:

java.lang.NullPointerException
   at com.zippyshare.search.free.fragment.MainFragment.loadFromAssetsWhite(MainFragment.java:501)
   at com.zippyshare.search.free.fragment.MainFragment.shouldInterceptRequest(MainFragment.java:477)
   at android.webkit.CallbackProxy.shouldInterceptRequest(CallbackProxy.java:1463)
   at android.webkit.BrowserFrame.shouldInterceptRequest(BrowserFrame.java:964)
   at android.webkit.JWebCoreJavaBridge.nativeServiceFuncPtrQueue(JWebCoreJavaBridge.java)
   at android.webkit.JWebCoreJavaBridge.handleMessage(JWebCoreJavaBridge.java:113)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:177)
   at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:1133)
   at java.lang.Thread.run(Thread.java:856)

这是崩溃代码:

    private WebResourceResponse loadFromAssetsWhite(String assetPath, String mimeType, String encoding){

501 line ------>AssetManager assetManager = getActivity().getAssets();
                InputStream input;
                try {
                    input = assetManager.open("search-white.png");
                    return new WebResourceResponse(mimeType, encoding, input);
                } catch (IOException e) {
                    Log.e("WEB-APP", "Error loading " + assetPath + " from assets: " +
                            e.getMessage(), e);
                }
                return null;
            }

这里是对该方法的调用:

if (webViewUrl != null && webViewUrl.contains("results.html")) {
                            return loadFromAssetsBlack("", "", "");
                        } else {
    477 line--------------->return loadFromAssetsWhite("", "", "");
                        }

完整的 WebViewClient 代码:

        webView.setWebViewClient(new WebViewClient()
            {
                private boolean mSuccess = true;

                @SuppressWarnings("deprecation")
                @Override
                public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
                    Logcat.d("Resource(PRE) URL: "+url);
                    if (url.contains("adcash.com")){
                        return new WebResourceResponse("text/plain", "utf-8",
                                new ByteArrayInputStream("".getBytes()));
                    } else if (url.equals("search_box_icon.png")) {
                        isUrlReady = false;
                        webViewUrl = null;
                        try {
                            MainActivity.mainActivity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    webViewUrl = webView.getUrl();
                                    isUrlReady = true;
                                }
                            });

                        } catch (Exception ex) {
                            ex.printStackTrace();
                            isUrlReady = true;
                        }
                        while(!isUrlReady);
                        isUrlReady = false;
                        if (webViewUrl != null && webViewUrl.contains("results.html")) {
                            return loadFromAssetsBlack("", "", "");
                        } else {
                            return loadFromAssetsWhite("", "", "");
                        }
                    }

                    Logcat.d("Resource(POST) URL: "+url);
                    return super.shouldInterceptRequest(view, url);
                }
    private WebResourceResponse loadFromAssetsBlack(String assetPath, String mimeType, String encoding){

                    AssetManager assetManager = getActivity().getAssets();
                    InputStream input;
                    try {
                        input = assetManager.open("search-black.png");
                        return new WebResourceResponse(mimeType, encoding, input);
                    } catch (IOException e) {
                        Log.e("WEB-APP", "Error loading " + assetPath + " from assets: " +
                                e.getMessage(), e);
                    }
                    return null;
                }



    private WebResourceResponse loadFromAssetsWhite(String assetPath, String mimeType, String encoding){

            AssetManager assetManager = getActivity().getAssets();
            InputStream input;
            try {
                input = assetManager.open("search-white.png");
                return new WebResourceResponse(mimeType, encoding, input);
            } catch (IOException e) {
                Log.e("WEB-APP", "Error loading " + assetPath + " from assets: " +
                        e.getMessage(), e);
            }
            return null;
        }

如果您需要更多详细信息,可以查看 Crashalytics 报告:http://crashes.to/s/e0975fe822c 提前感谢您的帮助。

按照@Chandra 的建议修复:

private WebResourceResponse loadFromAssetsWhite(String assetPath, String mimeType, String encoding){
            Context context = getActivity();
            if(context == null) {
                context = MainActivity.mainActivity;
            }
            AssetManager assetManager = context.getAssets();
            InputStream input;
            try {
                input = assetManager.open("search-white.png");
                return new WebResourceResponse(mimeType, encoding, input);
            } catch (IOException e) {
                Log.e("WEB-APP", "Error loading " + assetPath + " from assets: " +
                        e.getMessage(), e);
            }
            return null;
        }