编辑 - 更改了标题以反映真实问题。
一个非常奇怪的问题。 我有一个包含WebView的布局。除最近更新到Android 7.0的一部手机外,它的功能完美无缺。 我非常确定WebView不是一个问题,因为同一个应用程序中的其他网页浏览器仍能正常运行。
我开始认为这与要更改的片段中的布局有关。
mainactivity设置像这样的片段..
private void initFragment() {
orderFragment = new OrderFragment();
settingFragment = new SettingFragment();
driverListFragment = new DriverListFragment();
driverDetailFragment = new DriverDetailFragment();
fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.main_container, orderFragment, OrderFragment.class.getName());
ft.add(R.id.main_container, driverListFragment, DriverListFragment.class.getName());
ft.add(R.id.main_container, settingFragment, SettingFragment.class.getName());
String fromActivity = getIntent().getStringExtra(FROM_TO);
if (LANGUAGE_SETTING_PAGE.equals(fromActivity)) {
ft.hide(driverListFragment);
ft.hide(orderFragment);
ft.show(settingFragment);
group.check(R.id.main_setting);
mOption = 3;
} else {
mOption = 1;
ft.hide(driverListFragment);
ft.hide(settingFragment);
ft.show(orderFragment);
group.check(R.id.main_order);
}
ft.commit();
}
当按下菜单项时,我们这样做......
case R.id.main_order:
mOption = 1;
flushOrderListData();
ft.show(orderFragment);
ft.hide(settingFragment);
ft.hide(driverListFragment);
break;
flushOrderListData方法......
private void flushOrderListData() {
mWebViewProgress.post(new Runnable() {
@Override
public void run() {
mWebViewProgress.addJavascriptInterface(MainActivity.this, "android");
mWebViewProgress.loadUrl(mUrl);
orderFragment.layout.removeView(mWebViewProgress);
orderFragment.layout.addView(mWebViewProgress);
}
});
}
WebViewProgress是一个扩展WebView的类。
我100%确定问题不存在,我想在不同的布局中显示的网址是否正常。
这是XML .. 我尝试删除除了webview之外的所有内容,但这没有任何区别。
基本上,绝对没有任何内容,也没有报告错误。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="64dp"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:visibility="gone" >
<TextView
android:id="@+id/main_top_order_all"
style="@style/main_top_order_style"
android:text="@string/main_top_order_all" />
<View
android:layout_width="1dp"
android:layout_height="wrap_content"
android:background="@color/bg_gray" />
<TextView
android:id="@+id/main_top_order_money"
style="@style/main_top_order_style"
android:text="@string/main_top_order_money" />
<View
android:layout_width="1dp"
android:layout_height="wrap_content"
android:background="@color/bg_gray" />
<TextView
android:id="@+id/main_top_order_card"
style="@style/main_top_order_style"
android:text="@string/main_top_order_card" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/bg_gray" />
<LinearLayout
android:id="@+id/ll_show_older"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<com.city.home.view.WebViewProgress
android:id="@+id/web_order"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
public class WebViewProgress extends WebView {
private ProgressBar progressbar;
private View view_layout;
public WebViewProgress(Context context) {
super(context);
init(context);
}
public WebViewProgress(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
@Override
public void loadUrl(String url) {
super.loadUrl(url);
Log.i("Loading ",url);
}
private void init(final Context context) {
Log.i("Webview","init");
progressbar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 4);
progressbar.setLayoutParams(params);
addView(progressbar);
setJavaScript();
setWebViewClient(new WebViewClient() {
@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error){
Log.i("WebView","received SSL");
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Problem with SSL Certificate");
builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.proceed();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.cancel();
}
});
final AlertDialog dialog = builder.create();
dialog.show();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(BuildConfig.DEBUG) {
Log.i("WebView", url + "");
}
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if(BuildConfig.DEBUG) {
Log.i("WebViewPageFinished", url);
}
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.i("WebViewPageStarted", url);
}
});
setWebChromeClient(new WebChromeClient() {
public boolean onConsoleMessage(ConsoleMessage cm) {
Log.i("WebView","setWebChromeClient");
if(BuildConfig.DEBUG) {
Log.d("Console",cm.message()+" "+cm.lineNumber());
}
return true;
}
});
}
public class WebViewClient extends android.webkit.WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(BuildConfig.DEBUG) {
Log.i("WebView", url + "");
}
view.loadUrl(url);
return true;
}
}
public class WebChromeClient extends android.webkit.WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
if (view_layout != null) {
view_layout.setVisibility(View.GONE);
}
progressbar.setVisibility(GONE);
} else {
if (progressbar.getVisibility() == GONE){
progressbar.setVisibility(VISIBLE);
}
progressbar.setProgress(newProgress);
}
super.onProgressChanged(view, newProgress);
}
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
if (Share.debug)
Share.d("alert:" + message);
return false;
}
}
/**
* 设置支持JavaScript
*/
public void setJavaScript() {
Log.i("WebView","setJavaScript");
WebSettings ws = this.getSettings();
ws.setJavaScriptEnabled(true);
ws.setJavaScriptCanOpenWindowsAutomatically(true);
ws.setDomStorageEnabled(true);
}
OrderFragment
@SuppressLint("JavascriptInterface")
public class OrderFragment extends Fragment {
private TextView main_top_order_all;
private TextView main_top_order_money;
private TextView main_top_order_card;
public LinearLayout layout;
public WebViewProgress mWeb;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_faq, null);
layout = (LinearLayout) view.findViewById(R.id.layout);
mWeb = (WebViewProgress) view.findViewById(R.id.web_order);
main_top_order_all = (TextView) view.findViewById(R.id.main_top_order_all);
main_top_order_money = (TextView) view.findViewById(R.id.main_top_order_money);
main_top_order_card = (TextView) view.findViewById(R.id.main_top_order_card);
loadData();
return view;
}
/**
* 加载数据
*/
public void loadData() {
String default_all = getResources().getString(R.string.main_top_order_all);
String default_money = getResources().getString(R.string.main_top_order_money);
String default_card = getResources().getString(R.string.main_top_order_card);
int spColor = getResources().getColor(R.color.orange);
main_top_order_all.setText(SpannalTextManager.getInstance().getTextHieght(default_all, "0/0.00", spColor));
main_top_order_money.setText(SpannalTextManager.getInstance().getTextHieght(default_money, "0/0.00", spColor));
main_top_order_card.setText(SpannalTextManager.getInstance().getTextHieght(default_card, "0/0.00", spColor));
}
public static OrderFragment newInstance() {
OrderFragment fragment = new OrderFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
}
答案 0 :(得分:2)
我不确定问题是什么,但...... 我读到如果视图无法附加到窗口,那么附加到视图上的runnable的代码将不会被执行。所以,这...
mWebViewProgress.post(new Runnable() {
@Override
public void run() {
mWebViewProgress.addJavascriptInterface(MainActivity.this, "android");
mWebViewProgress.loadUrl(mUrl);
orderFragment.layout.removeView(mWebViewProgress);
orderFragment.layout.addView(mWebViewProgress);
}
});
改为此......
Handler handler = new Handler();
//mWebViewProgress.post(new Runnable() {
handler.post(new Runnable() {
@Override
public void run() {
mWebViewProgress.loadUrl(mUrl);
mWebViewProgress.addJavascriptInterface(MainActivity.this, "android");
orderFragment.layout.removeView(mWebViewProgress);
orderFragment.layout.addView(mWebViewProgress);
}
});
}
瞧,瞧!它工作正常。
我想一些新代码微妙地改变了视图创建的时间。 (我只是在这里猜测。)