webview 设置中使用webview的方法,(系统应用无法使用webview),运行直接crash,log如下
Process: com.android.settings, PID: 26979
android.view.InflateException: Binary XML file line #29 in com.android.settings:layout/dialog_layout_protocol: Binary XML file line #29 in com.android.settings:layout/dialog_layout_protocol: Error inflating class android.webkit.WebView
Caused by: android.view.InflateException: Binary XML file line #29 in com.android.settings:layout/dialog_layout_protocol: Error inflating class android.webkit.WebView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:852)
at android.view.LayoutInflater.createView(LayoutInflater.java:774)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:928)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:948)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1002)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at com.android.settings.LegalSettings.openLenovoExpProtocol(LegalSettings.java:96)
at com.android.settings.LegalSettings.onPreferenceClick(LegalSettings.java:83)
at androidx.preference.Preference.performClick(Preference.java:1186)
at androidx.preference.Preference.performClick(Preference.java:1168)
at androidx.preference.Preference$1.onClick(Preference.java:181)
at android.view.View.performClick(View.java:7503)
at android.view.View.performClickInternal(View.java:7480)
at android.view.View.access$3600(View.java:813)
at android.view.View$PerformClick.run(View.java:28445)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7945)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:603)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.UnsupportedOperationException: For security reasons, WebView is not allowed in privileged processes
at android.webkit.WebViewFactory.getProvider(WebViewFactory.java:246)
at android.webkit.WebView.getFactory(WebView.java:2576)
at android.webkit.WebView.ensureProviderCreated(WebView.java:2570)
at android.webkit.WebView.setOverScrollMode(WebView.java:2638)
at android.view.View.<init>(View.java:5360)
at android.view.View.<init>(View.java:5512)
at android.view.ViewGroup.<init>(ViewGroup.java:699)
at android.widget.AbsoluteLayout.<init>(AbsoluteLayout.java:56)
at android.webkit.WebView.<init>(WebView.java:413)
at android.webkit.WebView.<init>(WebView.java:355)
at android.webkit.WebView.<init>(WebView.java:337)
at android.webkit.WebView.<init>(WebView.java:324)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:852)
at android.view.LayoutInflater.createView(LayoutInflater.java:774)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:928)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:948)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1002)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at com.android.settings.LegalSettings.openLenovoExpProtocol(LegalSettings.java:96)
at com.android.settings.LegalSettings.onPreferenceClick(LegalSettings.java:83)
at androidx.preference.Preference.performClick(Preference.java:1186)
at androidx.preference.Preference.performClick(Preference.java:1168)
at androidx.preference.Preference$1.onClick(Preference.java:181)
at android.view.View.performClick(View.java:7503)
at android.view.View.performClickInternal(View.java:7480)
at android.view.View.access$3600(View.java:813)
at android.view.View$PerformClick.run(View.java:28445)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7945)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:603)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
使用网上的解决办法依旧无效
2.使用fileprovider
未尝试,应该是问题不大
3.使用io流的方式读取
TextView需要添加如下属性 android:autoLink="web"
public InputStreamReader getStreamReader(String filename) {
try {
InputStreamReader inputStreamReader = new InputStreamReader(getResources().getAssets().open(filename), filename.equals(filename) ? "UTF-8" : "GBK");
return inputStreamReader;
} catch (IOException e) {
return null;
}
}
TextView textView = findViewById(R.id.tv_text);
Locale locale = Locale.getDefault();
String language=locale.getLanguage();
String filename="";
switch (language){
case "zh":
filename="zh_rn/index.html";
break;
default:
filename="en/index.html";
break;
}
Log.d("majulian","language"+language);
try {
InputStreamReader inputStreamReader = getStreamReader(filename);
StringBuffer stringBuffer = new StringBuffer();
char[] chars = new char[4096];
for (int i; (i = inputStreamReader.read(chars)) != -1; ) {
stringBuffer.append(new String(chars, 0, i));
}
Spanned spanned = Html.fromHtml(stringBuffer.toString());
String aa = spanned.toString();
textView.setText(spanned);
} catch (IOException e) {
e.getStackTrace();
}
文件在该目录下
参考链接如下:
https://blog.csdn.net/hard_working1/article/details/70057383