Tomblarom
Tomblarom

Reputation: 1458

Binary XML file line #41: Error inflating class fragment

Error in Log: pastebin.com/YLvmkAd4

FragmentSettings.class:

package com.android.app;

import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.PreferenceFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentSettings extends PreferenceFragment {

    [..]

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
            mListPreference = (ListPreference) getPreferenceManager().findPreference("preference_key");
    
            return inflater.inflate(R.layout.activity_main, container, false);
        }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar_actionbar"
            layout="@layout/toolbar_default"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:clickable="true"
            android:layout_height="match_parent">
            <ListView
                android:id="@android:id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </ListView>
        </FrameLayout>
    </LinearLayout>
    <!-- android:layout_marginTop="?android:attr/actionBarSize"-->
    <com.android.app.ScrimInsetsFrameLayout
        android:id="@+id/scrimInsetsFrameLayout"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:layout_gravity="start"
        app:insetForeground="#4000"
        android:elevation="10dp">

        <fragment
            android:id="@+id/fragment_drawer"
            android:name="com.android.app.NavigationDrawerFragment"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout="@layout/fragment_navigation_drawer" />
    </com.android.app.ScrimInsetsFrameLayout>


</android.support.v4.widget.DrawerLayout>

Devices:

Oppo N1, Android 4.2.2
Samsung Galaxy Note 1, Android 5.1.1

I've spent now hours and hours for this error and couldn't find it. The clue is that exactly the same app is working perfectly on Android 5.1.1.

Additional:

MainActivity.java pastebin.com/a2NZLZFm

NavigationDrawerFragment.java pastebin.com/PqTMiPp3

fragment_navigation_drawer.xml pastebin.com/eVeAdv17

Upvotes: 2

Views: 1979

Answers (3)

Shail Shah
Shail Shah

Reputation: 11

Add this line into your styles.xml file.

<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>

Upvotes: 0

Tomblarom
Tomblarom

Reputation: 1458

Well you all helped me a lot, but I fixed it by myself ;)

I changed..

public class FragmentSettings extends PreferenceFragment {

    public ListPreference mListPreference;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mListPreference = (ListPreference) getPreferenceManager().findPreference("preference_key");
        return inflater.inflate(R.layout.activity_main, container, false);
    }
}

To..

public class FragmentSettings extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }

}

And removed with this onCreateView and mListPreference :) Hope this help others ;)

Upvotes: 0

Elltz
Elltz

Reputation: 10869

<fragment
     android:id="@+id/fragment_drawer"
     android:name="com.android.app.NavigationDrawerFragment"
     android:layout_width="@dimen/navigation_drawer_width"
     android:layout_height="match_parent"
     android:fitsSystemWindows="true"/>

use this rather, and inflate the layout in your onCreateView() android will take care of adding it ViewGroup/window etc etc

it is this line in your MainActivity

 // Set up the drawer.
    mNavigationDrawerFragment.setup(R.id.fragment_drawer, 
             (DrawerLayout) findViewById(R.id.drawer), mToolbar);

remove it and the error will go. Now the next thing is you let the Activity handle the Drawerlayout himself. so transfer all drawer codes to your activity and forget the setup() method in your fragment

The reason is since you declared the fragment as embeded in the Activity, android initiates it for you, so you do not need to set it up

hope it works

Upvotes: 1

Related Questions