Android WebView 在 android 4.0 + 上崩潰應用程序 (Android WebView crashes application on android 4.0 +)


問題描述

Android WebView 在 android 4.0 + 上崩潰應用程序 (Android WebView crashes application on android 4.0 +)

I am having hard time running webview using my application as it crashes the app right way.

Java

package com.unext.unextlibrary;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;


  @SuppressLint("SetJavaScriptEnabled")
   public class InitializeVideo extends Activity {

WebView mWebview;

@Override
@JavascriptInterface
public void onCreate(Bundle icicle) {
    setContentView(R.layout.activity_video_play);

    getWindow().requestFeature(Window.FEATURE_PROGRESS);
    mWebview  = new WebView(this);

    mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript

    final Activity activity = this;

    mWebview.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
        }
    });

    mWebview .loadUrl("http://www.google.com");
    setContentView(mWebview );
}
}

XML CODE

<?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:background="#000000"
android:orientation="vertical" >

 <android.webkit.WebView android:id="@+id/WebView"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
</android.webkit.WebView>

</LinearLayout>

Inside my manifest i am targeting sdk version as 17 

Manifest goes here

<uses‑sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="17" />

  <uses‑permission android:name="android.permission.INTERNET"/>

 <activity
        android:theme="@style/Theme.Video"
        android:configChanges="orientation|screenSize"
        android:name="com.unext.unextlibrary.InitializeVideo"
        android:label="@string/app_name" >
     </activity> 

Also my logcat output shows as follow

01‑24 07:11:09.091: E/AndroidRuntime(2457): FATAL EXCEPTION: main
    01‑24 07:11:09.091: E/AndroidRuntime(2457): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.unext.unextlibrary/com.unext.unextlibrary.InitializeVideo}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
    01‑24 07:11:09.091: E/AndroidRuntime(2457):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    01‑24 07:11:09.091: E/AndroidRuntime(2457):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    01‑24 07:11:09.091: E/AndroidRuntime(2457):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    01‑24 07:11:09.091: E/AndroidRuntime(2457):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    01‑24 07:11:09.091: E/AndroidRuntime(2457):     at android.os.Handler.dispatchMessage(Handler.java:99)
    01‑24 07:11:09.091: E/AndroidRuntime(2457):     at android.os.Looper.loop(Looper.java:137)
    01‑24 07:11:09.091: E/AndroidRuntime(2457):     at android.app.ActivityThread.main(ActivityThread.java:5039)
    01‑24 07:11:09.091: E/AndroidRuntime(2457):     at java.lang.reflect.Method.invokeNative(Native Method)
    01‑24 07:11:09.091: E/AndroidRuntime(2457):     at java.lang.reflect.Method.invoke(Method.java:511)
    01‑24 07:11:09.091: E/AndroidRuntime(2457):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    01‑24 07:11:09.091: E/AndroidRuntime(2457):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    01‑24 07:11:09.091: E/AndroidRuntime(2457):     at dalvik.system.NativeStart.main(Native Method)
    01‑24 07:11:09.091: E/AndroidRuntime(2457): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
    01‑24 07:11:09.091: E/AndroidRuntime(2457):     at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:229)
    01‑24 07:11:09.091: E/AndroidRuntime(2457):     at com.unext.unextlibrary.InitializeVideo.onCreate(InitializeVideo.java:24)
    01‑24 07:11:09.091: E/AndroidRuntime(2457):     at android.app.Activity.performCreate(Activity.java:5104)
    01‑24 07:11:09.091: E/AndroidRuntime(2457):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    01‑24 07:11:09.091: E/AndroidRuntime(2457):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    01‑24 07:11:09.091: E/AndroidRuntime(2457):     ... 11 more

‑‑‑‑‑

參考解法

方法 1:

Try to change as below : 

  

setContentView(R.layout.activity_video_play);       getWindow().requestFeature(Window.FEATURE_PROGRESS);

Use this way instead: 

  

getWindow().requestFeature(Window.FEATURE_PROGRESS);       setContentView(R.layout.activity_video_play);

方法 2:

Try this way,

public void onCreate(Bundle icicle) {
getWindow().requestFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.activity_video_play);

its because,  requestFeature() must be called before  calling  setContentView();

方法 3:

Your code

public void onCreate(Bundle icicle) {
    setContentView(R.layout.activity_video_play);

Now put the comment this setContentView() statement as no need and add new statement

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    //setContentView(R.layout.activity_video_play);

As the request for the another window feature must set prior to set the content view so window was prepared for the content view which you are using it. Another point is either you used Layout for setContentView() or just put single component in this as you are using both first used the layout and then last set the webview this was over write the layout with webview

方法 4:

And also dont forget to use "android:hardwareAccelerated=true" in android manifest file 

especially for android 4 and above. 

(by Aman VirkGrIsHuAndro SelvaPratikShadow)

參考文件

  1. Android WebView crashes application on android 4.0 + (CC BY‑SA 3.0/4.0)

#android-webview #Android






相關問題

如何在我創建的 Android WebView 中播放動態 mp4 視頻 (How do I play dynamic mp4 video within Android WebView I created)

Phonegap 應用程序不滾動 (Phonegap Application Not Scrolling)

Android - 將本地圖像設置為 WebView 中的 img 元素 (Android - Set a Local Image to an img element in WebView)

使用 webView 的 loadDataWithBaseURL 不從 sdcard 加載圖像 (Using webView's loadDataWithBaseURL not loading images from sdcard)

WebView LoadURL dan GetView (WebView LoadURL and GetView)

Android WebView 在 android 4.0 + 上崩潰應用程序 (Android WebView crashes application on android 4.0 +)

Android: Chế độ xem hình ảnh chụm-thu phóng với Chế độ xem con được nối thêm (Android: Pinch-zooming ImageView with appended child Views)

android.webkit.WebView 無法轉換為 android.widget.Button (android.webkit.WebView cannot be cast to android.widget.Button)

使用 android studio 在 webview 中顯示谷歌表單 (display google forms in webview using android studio)

如何解決使用 WebView 時出現“無法打開數據庫文件”的錯誤? (How to solve error "unable to open database file" when using WebView?)

android webview 顯示其字體的默認字體系列是什麼? (What is the default font family taken by android webview to show its fonts?)

Android Studio - 視頻音量低 (Android Studio - Video Volume Low)







留言討論