WebResourceResponse 导致 ClassNotFoundException 忽略 @TargetApi 子句
WebResourceResponse cause ClassNotFoundException ignoring @TargetApi clause
我有一段简单的代码:
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if ( Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ) return null;
...
try {
File input = new File(fullPathToCachedFile);
if ( input.exists() == false ) return null;
final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(input));
return new WebResourceResponse(mimeType, "UTF-8", stream);
}
catch (IOException e) { e.printStackTrace(); }
...
return null;
}
这句话让我很生气:
return new WebResourceResponse(mimeType, "UTF-8", stream);
在 Android 4.4(例如)上,整个项目运行良好,但我在 Android 2.2 和 2.3 上尝试过。然后控制台说不知道 "WebResourceResponse" class.
Thread [<1> main] (Suspended (exception ClassNotFoundException))
<VM does not provide monitor information>
PathClassLoader.findClass(String) line: 243
PathClassLoader(ClassLoader).loadClass(String, boolean) line: 573
PathClassLoader(ClassLoader).loadClass(String) line: 532
WebViewExt.defaultConfig() line: 52 (here is about to use WebResourceResponse)
WebViewExt.init(ForegroundService) line: 35
ForegroundService.startWebView(WebViewExt) line: 88
MyApplication.onServiceConnected(ComponentName, IBinder) line: 78
ActivityThread$PackageInfo$ServiceDispatcher.doConnected(ComponentName, IBinder) line: 1247
ActivityThread$PackageInfo$ServiceDispatcher$RunConnection.run() line: 1264
ActivityThread$H(Handler).handleCallback(Message) line: 587
ActivityThread$H(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626
NativeStart.main(String[]) line: not available [native method]
为什么我知道,它是关于 "WebResourceResponse" class?未提及线路项目运行良好:
/*return new WebResourceResponse(mimeType, "UTF-8", stream);*/
没有它项目运行良好,没有错误和异常。
@TargetAPI 不应该阻止此 "WebResourceResponse" Class 对 Android 的可见性?它是如何工作的以及如何解决这个问题?
我以某种方式修复了它。但这是奇怪的修复。 if 语句
if ( Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ) return null;
在 shouldInterceptRequest 函数内部不起作用,但在设置 WebViewClient 自定义之外起作用 class:
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB )
setWebViewClient(new WebViewClientExt2(this)); //with shouldInterceptRequest
else
setWebViewClient(new WebViewClientExt(this)); //without
我有一段简单的代码:
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if ( Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ) return null;
...
try {
File input = new File(fullPathToCachedFile);
if ( input.exists() == false ) return null;
final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(input));
return new WebResourceResponse(mimeType, "UTF-8", stream);
}
catch (IOException e) { e.printStackTrace(); }
...
return null;
}
这句话让我很生气:
return new WebResourceResponse(mimeType, "UTF-8", stream);
在 Android 4.4(例如)上,整个项目运行良好,但我在 Android 2.2 和 2.3 上尝试过。然后控制台说不知道 "WebResourceResponse" class.
Thread [<1> main] (Suspended (exception ClassNotFoundException))
<VM does not provide monitor information>
PathClassLoader.findClass(String) line: 243
PathClassLoader(ClassLoader).loadClass(String, boolean) line: 573
PathClassLoader(ClassLoader).loadClass(String) line: 532
WebViewExt.defaultConfig() line: 52 (here is about to use WebResourceResponse)
WebViewExt.init(ForegroundService) line: 35
ForegroundService.startWebView(WebViewExt) line: 88
MyApplication.onServiceConnected(ComponentName, IBinder) line: 78
ActivityThread$PackageInfo$ServiceDispatcher.doConnected(ComponentName, IBinder) line: 1247
ActivityThread$PackageInfo$ServiceDispatcher$RunConnection.run() line: 1264
ActivityThread$H(Handler).handleCallback(Message) line: 587
ActivityThread$H(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626
NativeStart.main(String[]) line: not available [native method]
为什么我知道,它是关于 "WebResourceResponse" class?未提及线路项目运行良好:
/*return new WebResourceResponse(mimeType, "UTF-8", stream);*/
没有它项目运行良好,没有错误和异常。
@TargetAPI 不应该阻止此 "WebResourceResponse" Class 对 Android 的可见性?它是如何工作的以及如何解决这个问题?
我以某种方式修复了它。但这是奇怪的修复。 if 语句
if ( Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ) return null;
在 shouldInterceptRequest 函数内部不起作用,但在设置 WebViewClient 自定义之外起作用 class:
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB )
setWebViewClient(new WebViewClientExt2(this)); //with shouldInterceptRequest
else
setWebViewClient(new WebViewClientExt(this)); //without