0

アプリが正しくクラッシュするため、アプリケーションを使用してWebViewを実行するのに苦労しています。

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コード

<?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>

マニフェスト内で、SDKバージョンを17としてターゲットにしています

マニフェストはここに行きます

<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> 

また、logcatの出力は次のように表示されます

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
4

4 に答える 4

2

以下のように変更してみてください:

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

代わりに次の方法を使用します。

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

于 2013-01-24T07:14:58.633 に答える
1

この方法を試してください、

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

そのため、requestFeature()を呼び出す前に呼び出す必要があります setContentView();

于 2013-01-24T07:13:05.960 に答える
0

また"android:hardwareAccelerated=true"、Androidマニフェストファイルで使用することを忘れないでください

特にAndroid 4以降の場合。

于 2013-01-24T07:21:02.243 に答える
0

あなたのコード

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

ここで、この setContentView() ステートメントを不要としてコメントを入れ、新しいステートメントを追加します

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

別のウィンドウ機能の要求は、コンテンツ ビューを設定する前に設定する必要があるため、使用しているコンテンツ ビュー用にウィンドウが準備されました。もう1つのポイントは、レイアウトを使用したsetContentView()か、最初にレイアウトを使用してから最後に設定した両方を使用しているため、これに単一のコンポーネントをwebview配置したことです。webview

于 2013-01-24T07:19:28.477 に答える