关于此问题有多个问题,但是我所读过的所有修补程序似乎都没有起作用。

似乎无效的修复:

Fix 1: Android - InflateException: Binary XML file line #7

Fix 2: InflateException Binary XML file at line #7: Error inflating class fragment

问题描述:

我正在尝试将SliderView合并到我的玻璃器皿中。 SliderView可用作自定义加载屏幕。

1. Link I found-2. Google Review Team Proposed Link

我目前正在使用第一个链接。

在我的应用程序中,我调用SearchActivity.java来完成一些事情。我已经注释了大多数代码以查看问题,并且看来问题首先出现在这里:

protected void onCreate(Bundle bundle) {

        super.onCreate(bundle);
        setContentView(R.layout.activity_main);
}


这是可以在我的activity_main.xml中找到的:位于我的res / layout /中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black">

<!-- The line right under is line number 7 -->
    <com.google.android.glass.sample.apidemo.card.SliderView
        android:id="@+id/indeterm_slider"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:contentDescription="@string/banner_150"
        android:src="@drawable/websurg_icon_white_150" />

</RelativeLayout>


是的,我的SliderView位于我的com.google.android.glass.sample.apidemo.card包中。
是的,我的SliderView确实具有三个不同的必需构造函数。

public SliderView(Context paramContext) {
    this(paramContext, null);
}

public SliderView(Context paramContext, AttributeSet paramAttributeSet) {
    this(paramContext, paramAttributeSet, 0);
}

public SliderView(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
    super(paramContext, paramAttributeSet, paramInt);
    LayoutInflater.from(getContext()).inflate(R.layout.slider, this);
    this.slider = findViewById(R.id.slider_control);
    this.indeterminateSlider = ((ImageView) findViewById(R.id.indeterm_slider));
    hideSlider(false);
    hideIndeterminateSlider(false);
}


我遇到两个具体问题,那就是:


  java.lang.RuntimeException:无法启动活动
  ComponentInfo {com.google.android.glass.sample.apidement / com.google.android.glass.sample.apidemeo.card.SearchActivity}:





  android.view.InflateException:二进制XML文件第7行:错误
  充气课
  com.google.android.glass.sample.apidemo.card.SliderView


LogCat错误:

11-25 14:39:00.037: W/dalvikvm(3900): threadid=1: thread exiting with uncaught exception (group=0x41641bd8)
11-25 14:39:00.044: E/AndroidRuntime(3900): FATAL EXCEPTION: main
11-25 14:39:00.044: E/AndroidRuntime(3900): Process: com.google.android.glass.sample.apidemo, PID: 3900
11-25 14:39:00.044: E/AndroidRuntime(3900): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.glass.sample.apidemo/com.google.android.glass.sample.apidemo.card.SearchActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class com.google.android.glass.sample.apidemo.card.SliderView
11-25 14:39:00.044: E/AndroidRuntime(3900):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2273)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at android.app.ActivityThread.access$800(ActivityThread.java:138)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1236)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at android.os.Looper.loop(Looper.java:149)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at android.app.ActivityThread.main(ActivityThread.java:5045)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at java.lang.reflect.Method.invokeNative(Native Method)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at java.lang.reflect.Method.invoke(Method.java:515)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at dalvik.system.NativeStart.main(Native Method)
11-25 14:39:00.044: E/AndroidRuntime(3900): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.google.android.glass.sample.apidemo.card.SliderView
11-25 14:39:00.044: E/AndroidRuntime(3900):     at android.view.LayoutInflater.createView(LayoutInflater.java:620)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:294)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at android.app.Activity.setContentView(Activity.java:1929)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at com.google.android.glass.sample.apidemo.card.SearchActivity.onCreate(SearchActivity.java:80)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at android.app.Activity.performCreate(Activity.java:5235)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1089)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2188)
11-25 14:39:00.044: E/AndroidRuntime(3900):     ... 11 more
11-25 14:39:00.044: E/AndroidRuntime(3900): Caused by: java.lang.reflect.InvocationTargetException
11-25 14:39:00.044: E/AndroidRuntime(3900):     at java.lang.reflect.Constructor.constructNative(Native Method)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at android.view.LayoutInflater.createView(LayoutInflater.java:594)
11-25 14:39:00.044: E/AndroidRuntime(3900):     ... 22 more
11-25 14:39:00.044: E/AndroidRuntime(3900): Caused by: java.lang.ClassCastException: com.google.android.glass.sample.apidemo.card.SliderView cannot be cast to android.widget.ImageView
11-25 14:39:00.044: E/AndroidRuntime(3900):     at com.google.android.glass.sample.apidemo.card.SliderView.<init>(SliderView.java:52)
11-25 14:39:00.044: E/AndroidRuntime(3900):     at com.google.android.glass.sample.apidemo.card.SliderView.<init>(SliderView.java:45)
11-25 14:39:00.044: E/AndroidRuntime(3900):     ... 25 more


所以我的问题是,是什么导致这些错误?我需要进行哪些更改才能使其正常工作?

更新:

LayoutInflater.from(getContext()).inflate(R.layout.slider, this);
        this.slider = findViewById(R.id.slider_control);
        this.indeterminateSlider = ((ImageView) findViewById(R.id.indeterm_slider));


这是默认的res / layout / slider xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="@dimen/slider_bar_height" >

    <View
        android:id="@id/slider_control"
        android:layout_width="0.0px"
        android:layout_height="@dimen/slider_bar_height"
        android:background="@android:color/white" />

    <ImageView
        android:id="@id/indeterminate_slider"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/slider_bar_height"
        android:background="@drawable/slider_indeterminate" />

</FrameLayout>

最佳答案

更新:
解决方案如下:

在SliderView.java构造函数(public SliderView(Context paramContext, AttributeSet paramAttributeSet, int paramInt))中:

更改:

this.indeterminateSlider = ((ImageView) findViewById(R.id.indeterm_slider));


至:

this.indeterminateSlider = ((ImageView) findViewById(R.id.indeterminate_slider));


在您的slide.xml中(位于res / layout /中):

更改:

<ImageView
        android:id="@id/indeterminate_slider" [...] />


至:

<ImageView
    android:id="@+id/indeterminate_slider" [...] />


主要问题是让SliderView.java指向正确的ID。由于尚未完成,因此您收到了InflateException。解决此问题后,您将获得ClassCastException。这来自您的slide.xml没有+

indeterm_sliderSliderView,并且您要转换为ImageView。之所以得到InflateException,是因为由于该ClassCastException而无法创建视图

09-05 11:09