Search code examples
javaandroidxmlgoogle-glassgoogle-gdk

RuntimeException and InflateException: Binary XML file line #7 .. followed by ClassCastException


There are multiple questions about this problem, but none of the fixes I've read about seems to be working.

Fixes that didn't seem to work:

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

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

Description of my problem:

I'm trying in incorporate a SliderView in my glassware. SliderView can be used as a custom loading screen.

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

I'm currently using the first link.

During my application, I call upon SearchActivity.java which does a few things. I've commented most of the code to see the problem and it seems the problem first appears here:

protected void onCreate(Bundle bundle) {

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

Here is what can be found in my activity_main.xml: located in my 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>

Yes, my SliderView is located in my com.google.android.glass.sample.apidemo.card package. Yes, my SliderView does have the three different required constructors.

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);
}

I'm getting two specific problems and that is:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.glass.sample.apidement/com.google.android.glass.sample.apidemeo.card.SearchActivity}:

and

android.view.InflateException: Binary XML file line #7: Error inflating class com.google.android.glass.sample.apidemo.card.SliderView

LogCat Errors:

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

So my question is, what is causing these errors? What do I need to change for it to work?

UPDATE:

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

This is the default 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>

Solution

  • Update: The solution was the following:

    In your SliderView.java constructor ( public SliderView(Context paramContext, AttributeSet paramAttributeSet, int paramInt) ):

    Change:

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

    to:

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

    In your slider.xml (located in res/layout/ ) :

    Change:

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

    To:

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

    The main problem is getting your SliderView.java to point to the right id. Since this wasn't done you were getting a InflateException. Once that was fixed, you were getting a ClassCastException. This came from your slider.xml not having that +

    indeterm_slider is the SliderView, and you are casting to ImageView. You got the InflateException because your view can not be created due of this ClassCastException