android.view.WindowManager $BadTokenException异常.任何指针?

android.view.WindowManager $BadTokenException异常.任何指针?,第1张

概述我有两个网页浏览,并从网页浏览中删除了我使用超类中的所有方法创建超类的常用功能,并在两个网页视图中使用它. 一旦我创建对象并从第一个webview设置变量,WebView就会正常显示,当我按下“BACK”按钮并转到第二个WebView时,我得到了异常. 有帮助吗? 第一个WebView代码 public class firstWebView extends Activity { priv @H_301_4@ 我有两个网页浏览,并从网页浏览中删除了我使用超类中的所有方法创建超类的常用功能,并在两个网页视图中使用它.

一旦我创建对象并从第一个webvIEw设置变量,WebVIEw就会正常显示,当我按下“BACK”按钮并转到第二个WebVIEw时,我得到了异常.

有帮助吗?

第一个WebVIEw代码

public class firstWebVIEw extends Activity {    private static final String LOG_TAG = "FirstWebVIEw";    public static final int VIDEO_PLAY = 0;    private WebVIEw mWebVIEw;    private NicuWebVIEw _nicuWebVIEw;    private static final String URL = mainMenuActivity.urlSelected+"todo.HTML";        @OverrIDe    public voID onCreate(Bundle icicle) {        super.onCreate(icicle);        setContentVIEw(R.layout.webvIEw);        getwindow().setFeatureInt(Window.FEATURE_PROGRESS,Window.PROGRESS_VISIBIliTY_ON);        mWebVIEw = (WebVIEw) findVIEwByID(R.ID.webvIEw);        _nicuWebVIEw = NicuWebVIEw.getNicuWebVIEw(mWebVIEw);//      mWebVIEw.setWebChromeClIEnt(new MyWebChromeClIEnt());        final Activity activity = this;        NicuWebVIEw.setContext(activity);        setProgressbarVisibility(true);        int count = (int) _nicuWebVIEw.loadUrl(URL);        Toast.makeText(this,"Count = "+count,Toast.LENGTH_SHORT).show();    }public boolean onKeyDown(int keyCode,KeyEvent event) {    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebVIEw.canGoBack()) {//          startActivity(new Intent(getApplication(),mainMenuActivity.class));            mWebVIEw.goBack();            return true;        }        return super.onKeyDown(keyCode,event);    }

Code for Second与第一个类似,但URL指向不同的HTML页面.

E/AndroIDRuntime( 7913): androID.vIEw.WindowManager$BadTokenException: Unable to add window -- token androID.os.BinderProxy@447d2a98 is not valID; is your activity running?E/AndroIDRuntime( 7913):        at androID.vIEw.VIEwRoot.setVIEw(VIEwRoot.java:468)E/AndroIDRuntime( 7913):        at androID.vIEw.WindowManagerImpl.addVIEw(WindowManagerImpl.java:177)E/AndroIDRuntime( 7913):        at androID.vIEw.WindowManagerImpl.addVIEw(WindowManagerImpl.java:91)E/AndroIDRuntime( 7913):        at androID.vIEw.Window$LocalWindowManager.addVIEw(Window.java:424)E/AndroIDRuntime( 7913):        at androID.app.Dialog.show(Dialog.java:239)E/AndroIDRuntime( 7913):        at androID.app.AlertDialog$Builder.show(AlertDialog.java:802)E/AndroIDRuntime( 7913):        at androID.webkit.CallbackProxy.handleMessage(CallbackProxy.java:483)E/AndroIDRuntime( 7913):        at androID.os.Handler.dispatchMessage(Handler.java:99)E/AndroIDRuntime( 7913):        at androID.os.Looper.loop(Looper.java:123)E/AndroIDRuntime( 7913):        at androID.app.ActivityThread.main(ActivityThread.java:4363)E/AndroIDRuntime( 7913):        at java.lang.reflect.Method.invokeNative(NativeMethod)E/AndroIDRuntime( 7913):        at java.lang.reflect.Method.invoke(Method.java:521)E/AndroIDRuntime( 7913):        at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)E/AndroIDRuntime( 7913):        at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:618)E/AndroIDRuntime( 7913):        at dalvik.system.NativeStart.main(Native Method)

NicuWebVIEw的代码是

public class NicuWebVIEw {private static NicuWebVIEw _nicuWebVIEw;private static WebVIEw _webVIEw;private static HashMap<String,Long> _urls;private static Context contextname;private static final int VIDEO_PLAY = 0;private NicuWebVIEw(WebVIEw myWebVIEw) {    _urls = new HashMap<String,Long>(10);    _webVIEw = myWebVIEw;    _webVIEw.setWebVIEwClIEnt(new NicuWebVIEwClIEnt());    _webVIEw.getSettings().setJavaScriptEnabled(true);    _webVIEw.clearCache(true);    WebSettings webSettings = _webVIEw.getSettings();    webSettings.setSavePassword(true);    webSettings.setSaveFormData(true);    webSettings.setJavaScriptEnabled(true);    webSettings.setSupportZoom(true);    _webVIEw.addJavaScriptInterface(new DemoJavaScriptInterface(),"demo");    _webVIEw.clearCache(true);    _webVIEw.setWebVIEwClIEnt(new WebVIEwClIEnt() {    public voID onReceivedError(WebVIEw vIEw,int errorCode,String description,String failingUrl) {        Toast.makeText(contextname,"Super Class TOAST message",Toast.LENGTH_LONG).show();    }    @OverrIDe       public voID onPageFinished(WebVIEw vIEw,String url)       {           _webVIEw.loadUrl("JavaScript:(function () { " +                   "setvariable("+mainMenuActivity.numberSelected+");" +                 "})()");     }      });    _webVIEw.setWebChromeClIEnt(new WebChromeClIEnt() {        public voID onProgressChanged(WebVIEw vIEw,int progress) {          // ActivitIEs and WebVIEws measure progress with different scales.          // The progress meter will automatically disappear when we reach 100%            ((Activity) contextname).setProgress(progress * 1000);        }    });    _webVIEw.setWebVIEwClIEnt(new WebVIEwClIEnt() {        public voID onReceivedError(WebVIEw vIEw,String failingUrl) {          Toast.makeText(contextname,"Oh no! " + description,Toast.LENGTH_SHORT).show();        }     @OverrIDe       public voID onPageFinished(WebVIEw vIEw,String url)  {           _webVIEw.loadUrl("JavaScript:(function () { " +                   "setvariable("+mainMenuActivity.numberSelected+");" +                 "})()");     }    });}    // allow clicking on link to remain in app instead of launching androID browser    private class NicuWebVIEwClIEnt extends WebVIEwClIEnt {        @OverrIDe        public boolean shouldOverrIDeUrlLoading(WebVIEw vIEw,String url) {            vIEw.loadUrl(url);            return true;        }    }   public static NicuWebVIEw getNicuWebVIEw(WebVIEw myWebVIEw){    if (_nicuWebVIEw == null) _nicuWebVIEw = new NicuWebVIEw(myWebVIEw);    return _nicuWebVIEw;} // loads the requested URL and maintains a 'history' of pages visited.// returns number of times page was loaded. public long loadUrl(String url) {     long count = incrementCounter(url);     _webVIEw.loadUrl(url);      return count; } //helper to  private static long incrementCounter(String url){     if (url != null) {     if (_urls.get(url)==null) _urls.put(url,new Long(0));     long count = _urls.put(url,_urls.get(url) +1L);     return count;     }     else {         Toast.makeText(contextname,"Null URL",Toast.LENGTH_SHORT).show();         return 0;     } } /// return the number of times the given URL has been visited public long getUrlVisitCount(String url) {     if (_urls.get(url)==null) return 0L;     return _urls.get(url); } //returns to the prevIoUs URL,returns that URL public String goBack(){     if (_webVIEw.canGoBack()) _webVIEw.goBack();     String url = _webVIEw.getUrl();     incrementCounter(url);     return url; } //returns to next forward URL,returns that URL public String goForward(){    if (_webVIEw.canGoForward()) _webVIEw.goForward();    String url = _webVIEw.getUrl();    incrementCounter(url);    return url;      } public static boolean setContext(Context context) {     contextname = context;     return true; } public Context getContext(){     if (contextname == null) return null;     return contextname; }    final class DemoJavaScriptInterface {        public voID setPlayVIDeo(String option) {            Toast.makeText(contextname,"Playing VIDeo = "+option,Toast.LENGTH_SHORT).show();            Intent intent = new Intent ( contextname,playVIDeo.class );            ((Activity) contextname).startActivityForResult(intent,VIDEO_PLAY);        }    }       public boolean canGoBack() {        return _webVIEw.canGoBack();    }

}

@H_301_4@解决方法 问题是对话框需要具有您活动的“基础”上下文,而不一定是您从中启动它的那个.

这是一个经常有效的解决方案

Activity a = this;while(a.getParent() != null) {    a = a.getParent();}_nicuWebVIEw.setContext(a);

查看正在发生的事情的一种方法是修改该示例,如下所示

Activity a = this;while(a.getParent() != null) {    Log.i("ActivityTree",a.getClass().getSimplename());    a = a.getParent();}_nicuWebVIEw.setContext(a);

然后,adb将向您显示您所在的活动层次结构.

@H_301_4@ @H_301_4@ @H_301_4@ @H_301_4@ 总结

以上是内存溢出为你收集整理的android.view.WindowManager $BadTokenException异常.任何指针?全部内容,希望文章能够帮你解决android.view.WindowManager $BadTokenException异常.任何指针?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1123474.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存