原因:android.view.InflateException:二进制XML文件第11行:膨胀类io.flutter.view.FlutterView的错误

时间:2019-01-22 10:44:52

标签: java android xml android-studio flutter

这是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eeeeee"
android:theme="@style/Theme.AppCompat"
android:orientation="vertical">


<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_weight="1" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_weight="1">

    <TextView
        android:id="@+id/output"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingEnd="16dp"
        android:paddingLeft="16dp"
        android:paddingStart="16dp"
        android:paddingTop="16dp"
        android:textColor="@color/white" />

</RelativeLayout>

<io.flutter.view.FlutterView
    android:id="@+id/flutter_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    />

这是我的MainActivity.java

public class MainActivity extends FlutterActivity {

private static final String CHANNEL = "samples.flutter.io/jwplayer_view";
private static final String METHOD_SWITCH_VIEW = "switchView";
private static final int COUNT_REQUEST = 1;

private MethodChannel.Result result;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    GeneratedPluginRegistrant.registerWith(this);

    new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
            new MethodChannel.MethodCallHandler() {
                @Override
                public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
                    MainActivity.this.result = result;
                    int count = methodCall.arguments();
                    if (methodCall.method.equals(METHOD_SWITCH_VIEW)) {
                        onLaunchFullScreen(count);
                    } else {
                        result.notImplemented();
                    }
                }
            }
    );
}

private void onLaunchFullScreen(int count) {
    Intent fullScreenIntent = new Intent(this, JWPlayerFragmentExample.class);
    startActivityForResult(fullScreenIntent, COUNT_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == COUNT_REQUEST) {
        if (resultCode == RESULT_OK) {
            result.success(data.getIntExtra("counter",0));
        } else {
            result.error("ACTIVITY_FAILURE", "Failed while launching activity", null);
        }
    }
}
 }

StackTrace如下。

Shutting down VM
FATAL EXCEPTION: main
Process: com.neostencilmobileapp, PID: 16538
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.neostencilmobileapp/com.neostencilmobileapp.JWPlayerFragmentExample}: android.view.InflateException
  : Binary XML file line #11: Binary XML file line #11: Error inflating class io.flutter.view.FlutterView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3300)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3484)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2123)
    at android.os.Handler.dispatchMessage(Handler.java:109)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:7470)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class io.flutter.view.FlutterView
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class io.flutter.view.FlutterView
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
    at android.view.LayoutInflater.createView(LayoutInflater.java:658)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:801)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:874)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
    at com.neostencilmobileapp.JWPlayerFragmentExample.onCreate(JWPlayerFragmentExample.java:42)
    at android.app.Activity.performCreate(Activity.java:7436)
    at android.app.Activity.performCreate(Activity.java:7426)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1286)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3279)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3484)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2123)
    at android.os.Handler.dispatchMessage(Handler.java:109)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:7470)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
Caused by: java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.app.Activity
    at io.flutter.view.FlutterView.<init>(FlutterView.java:111)
    at io.flutter.view.FlutterView.<init>(FlutterView.java:105)
    ... 28 more

1 个答案:

答案 0 :(得分:0)

这似乎是由于抖动的必要初始化所致。下面的代码在加载抖动视图的活动中解决了我的问题。

io.flutter.view.FlutterMain.startInitialization(this.getApplicationContext());
String[] args = getArgsFromIntent(getIntent());
FlutterMain.ensureInitializationComplete(getApplicationContext(), args);