java - 启动新 Activity 时,带有 WebView 的 Android Activity 崩溃

标签 java android webview youtube

问题已更新:返回按钮的问题神秘地解决了。 现在开始新 Activity 时它会崩溃(全屏播放器)

我在四种条件下测试了我的应用:
1.在出现的WebView之前按下按钮:它工作正常
2. 在 WebView 中播放 YouTube 视频时按下按钮:效果很好
3. WebView播放完毕后按下按钮:效果很好
4.在 WebView 出现后但在单击播放按钮之前按下按钮:崩溃

我的代码如下:

@Override
public void onCreate(Bundle savedInstanceState) {
    // Remove notification bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.getWindow().setFlags(
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    super.onCreate(savedInstanceState); 
    webview = (WebView) findViewById(R.id.web_view_player_vertical);
    webview.clearCache(true);
    webview.clearHistory();
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setPluginState(PluginState.ON);
    webview.getSettings().setPluginsEnabled(true);
    webview.getSettings().setLoadWithOverviewMode(true);// completely zoomed out
    webview.getSettings().setUseWideViewPort(true);
    webview.getTouchables();
    webview.setBackgroundColor(Color.parseColor("#00000000"));
    webview.setWebChromeClient(new WebChromeClient());
    webview.setWebViewClient(new WebViewClient());
    webview.loadUrl(emdLink);
}

@Override
public void onPause() {
    super.onPause();
    try {
        Log.i("in onpause", "onpause try");
        Class.forName("android.webkit.WebView")
                .getMethod("onPause", (Class[]) null)
                .invoke(webview, (Object[]) null);
        Log.i("in onpause", "onpause tried");


    } catch (ClassNotFoundException cnfe) {
    } catch (NoSuchMethodException nsme) {
    } catch (InvocationTargetException ite) {
    } catch (IllegalAccessException iae) {
    } catch (Exception  all) {
        Log.i("Exception", "Exception : "+all );
    }

    webview.destroy();
}

public void FullScreenFunction(){
        Log.v("FullScreenFunction", "durationPass :: " + duration);
        Log.v("FullScreenFunction", SrcPath1);
        Bundle bundle = new Bundle();
        bundle.putString("duration", duration);
        bundle.putString("link", SrcPath1);
        bundle.putString("youtubeLink", youtubeLink);
        bundle.putString("thumbnailLink", thumbnailLink);
        bundle.putString("title", title);
        bundle.putString("id", VideoId);
        bundle.putString("view_count", viewCount);
        bundle.putString("uploaded_date", date);
        Log.v("FullScreenFunction", "start");

        Intent myIntent = new Intent(this, HdPlayerHorizontal.class);
        myIntent.putExtras(bundle);
        startActivity(myIntent);

}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Toast.makeText(this, "onDestroy", Toast.LENGTH_LONG).show();
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    Toast.makeText(this, "onStop", Toast.LENGTH_LONG).show();
}

@Override
public void onResume() {
    super.onResume();
    Log.v("onResume", "onResume");

}

任何形式的帮助将不胜感激。提前致谢!

以下是来自 logcat 的消息:

I/ActivityManager(1872): START {HdPlayerHorizontal (has extras)} from pid 13552
I/onPuase(13552): onPuase
I/in onpause(13552): onpause try
I/in onpause(13552): onpause tried
D/webviewglue(13552): nativeDestroy view: 0xc7b8d0
A/libc(13552): Fatal signal 11 (SIGSEGV) at 0x00d84c18 (code=2)

最佳答案

致命信号11是因为webview在你销毁它之后仍在绘制。只需将 webView.destroy() 放在 onStop 中即可。我相信您的应用程序会正常工作

关于java - 启动新 Activity 时,带有 WebView 的 Android Activity 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13061276/

相关文章:

java - DocumentListener 上的 JTable 过滤器不起作用

java - Java正则表达式中的混淆

java - 对象 ='statutProduits' 验证失败。错误计数 : 2

Java - 消息 header 值 : Basic 中的非法字符

android - 将 API 中的 MenuItem 文本取消大写 23

java - Android主线程不等待AsyncTask

android - 基于单一 Activity fragment 的应用程序和后退按钮

c# - 在 WebView Metro Style 应用程序中加载本地 html 文件

JavaFX WebView 无法加载某些网站

Android WebView JellyBean -> 不应该发生 : no rect-based-test nodes found