我有一个 PopupWindow,它在按下按钮时显示一个 WebView。如果 setBuiltInZoomControls 设置为false,WebView 将按预期显示页面。但是,如果 setBuiltInZoomControls 设置为true,则与 WebView 的任何交互(滚动、捏缩放)都会导致错误。
这是Java代码:
mHelpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.help_pop, null);
int width = WindowManager.LayoutParams.MATCH_PARENT;
int height = WindowManager.LayoutParams.WRAP_CONTENT;
final PopupWindow popupWindow = new PopupWindow(popupView, 1, 1, true);
popupWindow.setWindowLayoutMode(width,height);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), ""));
TextView helpText = (TextView) popupView.findViewById(R.id.help_text);
WebView webView = (WebView)popupView.findViewById(R.id.app_question_help_web_view);
if (!mQuestion.casiHelpText.contains("http://") && !mQuestion.casiHelpText.contains("<")) {
helpText.setText(mQuestion.casiHelpText);
popupWindow.showAsDropDown(mHelpButton);
} else if (mQuestion.casiHelpText.contains("http://") && !mQuestion.casiHelpText.contains("<")) {
if ((getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) ==
Configuration.SCREENLAYOUT_SIZE_XLARGE) {
Log.d(TAG,"Large size screen targeted");
webView.setVisibility(View.VISIBLE);
helpText.setVisibility(View.GONE);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl(mQuestion.casiHelpText);
findViewById(R.id.app_questions_activity_layout).post(new Runnable() {
public void run() {
popupWindow.showAsDropDown(mHelpButton);
}
});
}
else {
Intent i = new Intent(QuestionsActivity.this, WebViewActivity.class);
i.setData(Uri.parse(mQuestion.casiHelpText));
startActivity(i);
}
} else {
if ((getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) ==
Configuration.SCREENLAYOUT_SIZE_XLARGE) {
webView.setVisibility(View.VISIBLE);
helpText.setVisibility(View.GONE);
String baseHtml = "<html><meta name=\"viewport\" content=\"width=200\"/><head><title></title></head><body>" + mQuestion.casiHelpText + "</body><html>";
webView.loadDataWithBaseURL(null, baseHtml, "text/html", "UTF-8", null);
popupWindow.showAsDropDown(mHelpButton);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
} else {
Intent i = new Intent(QuestionsActivity.this, WebViewActivity.class);
i.putExtra("HTML", mQuestion.casiHelpText);
startActivity(i);
}
}
}
});
这是我的 Layout.xml (sw600dp)。只包含一个 Webview 和一个 TextView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:gravity="center"
android:background="@android:color/darker_gray">
<WebView
android:id="@+id/app_question_help_web_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:visibility="gone"/>
<TextView
android:id="@+id/help_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="testestestetstetst"
android:textColor="@android:color/white"
android:layout_below="@+id/app_question_help_web_view"
/>
这是错误:
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.******, PID: 19046
android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@4241c6c0 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:748)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:278)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.widget.ZoomButtonsController.setVisible(ZoomButtonsController.java:373)
at com.android.org.chromium.android_webview.AwZoomControls.invokeZoomPicker(AwZoomControls.java:28)
at com.android.org.chromium.content.browser.ContentViewCore.invokeZoomPicker(ContentViewCore.java:2607)
at com.android.org.chromium.content.browser.ContentViewGestureHandler$1.onScroll(ContentViewGestureHandler.java:429)
at com.android.org.chromium.content.browser.third_party.GestureDetector.onTouchEvent(GestureDetector.java:594)
at com.android.org.chromium.content.browser.ContentViewGestureHandler.processTouchEvent(ContentViewGestureHandler.java:970)
at com.android.org.chromium.content.browser.ContentViewGestureHandler.onTouchEvent(ContentViewGestureHandler.java:802)
at com.android.org.chromium.content.browser.ContentViewCore.onTouchEvent(ContentViewCore.java:1244)
at com.android.org.chromium.android_webview.AwContents.onTouchEvent(AwContents.java:1523)
at com.android.webview.chromium.WebViewChromium.onTouchEvent(WebViewChromium.java:1833)
at android.webkit.WebView.onTouchEvent(WebView.java:2167)
at android.view.View.dispatchTouchEvent(View.java:8115)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2129)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
at android.widget.PopupWindow$PopupViewContainer.dispatchTouchEvent(PopupWindow.java:1979)
at android.view.View.dispatchPointerEvent(View.java:8323)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4623)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4491)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4049)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4103)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4072)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4183)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4080)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4240)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4049)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4103)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4072)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4080)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4049)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6415)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6334)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6305)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6270)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6495)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6468)
at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6514)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java: