0

This crash is being reported sporadically and I can't explain why it would happen or repro it at all myself. I have an activity container which hosts a fragment in a frame layout that gets replaced as the user traverses the app, a bottom bar fragment which stays there all the time, and a standard tool bar and nav drawer. The error occurs when the bottom bar / footer fragment (called the ActionsFragment in my code), sometimes cannot be inflated.

Full activity xml:

<RelativeLayout
android:id="@+id/main_parent_view"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/wholeScreen"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/screenMinusBar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

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

                    <FrameLayout
                        android:id="@+id/fragmentContainer"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:paddingTop="?attr/actionBarSize"/>

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:scaleType="fitXY"
                        android:src="@drawable/action_fragment_shadow"/>
                </RelativeLayout>

                <fragment android:name="com.xyz.fragments.ActionsFragment"
                    android:id="@+id/actions_bottom_fragment"
                    android:layout_width="match_parent"
                    android:layout_height="56dp"/>
            </LinearLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?colorPrimary"
                android:minHeight="?attr/actionBarSize"
                app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                app:popupTheme="@style/Toolbar_Popup"/>

        </RelativeLayout>

        <com.devspark.robototextview.widget.RobotoTextView
            android:id="@+id/action_text_view_uid"
            style="?android:textAppearanceSmallInverse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:gravity="center"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:textColor="@color/white"
            android:visibility="invisible"
            app:typeface="roboto_light"/>
    </FrameLayout>

    <!-- The navigation drawer -->
    <LinearLayout
        android:id="@+id/left_drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="start"
        android:background="@color/white">

        <ListView
            android:id="@+id/left_drawer_list"
            android:layout_width="@dimen/nav_drawer_width"
            android:layout_height="wrap_content"
            android:listSelector="@android:color/transparent"
            android:choiceMode="singleChoice"
            android:cacheColorHint="#0000"
            android:paddingBottom="8dp"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="@color/divider"
            android:paddingTop="8dp"/>

        <LinearLayout
            android:id="@+id/nav_list_settings"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_marginTop="8dp"
            android:clickable="true"
            android:background="@drawable/nav_item_selector"
            android:orientation="horizontal">

                <ImageView
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:layout_gravity="center"
                    android:background="@null"
                    android:layout_marginLeft="16dp"
                    android:src="@drawable/ic_nav_settings"/>

                <com.devspark.robototextview.widget.RobotoTextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical"
                    android:minHeight="@dimen/listPreferredItemHeightSmall"
                    android:paddingLeft="32dp"
                    android:text="Help and Feedback"
                    android:textColor="@color/text"
                    android:textSize="14sp"
                    app:typeface="roboto_medium"/>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/white"/>
    </LinearLayout>

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

ActionsFragment.java basically just has some click listeners and two image buttons in a single layout, nothing too fancy, here's the majority of it:

public class ActionsFragment extends Fragment {

private static final int ANIMATION_DURATION = 200;
private Context mContext;
private MainActivity mHostingActivity;
private int screenWidth, screenHeight;
private TextView mUIDTextView;
private BitmapFactory.Options mBitmapOptions;
private Bitmap qrBitmap;
private Animator mCurrentAnimator;
private ImageView mImageButton1;
private ImageButton mImageButton2;
private int mImageButtonHeight = 64;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_action_bar_bottom, container, false);
    mContext = getActivity();

    DisplayMetrics metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    switch (metrics.densityDpi) {
        case DisplayMetrics.DENSITY_MEDIUM:
            mImageButtonHeight = 48;
            break;
        case DisplayMetrics.DENSITY_HIGH:
            mImageButtonHeight = 72;
            break;
        case DisplayMetrics.DENSITY_XHIGH:
            mImageButtonHeight = 96;
            break;
        case DisplayMetrics.DENSITY_XXHIGH:
            mImageButtonHeight = 144;
            break;
    }

    assert view != null;
    mImageButton2 = (ImageButton) view.findViewById(R.id.action_btn_1);
    mImageButton1 = (ImageButton) view.findViewById(R.id.action_btn_2);

    mHostingActivity = (MainActivity) getActivity();

    setupMetrics();
    setup();
    return view;
}

@Override
public void onStart(){
    super.onStart();
    mUIDTextView = (TextView) getActivity().findViewById(R.id.action_text_view_uid);
    mUIDTextView.setText("User Number:\n"
            + App.getDatabaseHandler().getTableLoggedUserID() + "\n");
}

private void setupMetrics() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        // the results will be higher than using the activity context object or the getWindowManager() shortcut
        WindowManager wm = (WindowManager) mContext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(displayMetrics);
        screenWidth = displayMetrics.widthPixels;
        screenHeight = displayMetrics.heightPixels;
    }

 private void setup() {
        try {
            if (App.isUserLoggedIn()) {

                User user = App.getDatabaseHandler().getUser();
                String uid = user.getUID();
                File imgFile = new File(uid);

                //if the image exists load it from memory
                if (imgFile.exists()) {
                    mBitmapOptions = new BitmapFactory.Options();
                    mBitmapOptions.inSampleSize = 1;
                }

                int newWidth = screenWidth / 3;
                newWidth = newWidth * 2;
                int newHeight = screenHeight / 3;
                newHeight = newHeight * 2;

                //create a qr for the image button
                if (!imgFile.exists()) {
                    String id = user.getId();
                    BarcodeGenerator b = new BarcodeGenerator(id, newWidth, newHeight, mContext);
                    b.createqr();
                }

                bm = BitmapFactory.decodeFile(user.getQr(), mBitmapOptions);
                mImageButton1.setImageBitmap(Bitmap.createScaledBitmap(bm, mImageButtonHeight, mImageButtonHeight, false));
                mImageButton1.invalidate();

                getActivity().onContentChanged();

                mImageButton1.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        //Launch activity
                    }
                });
            } else {
                // If the user is not logged in, they are returned to the Login screen of the application.
                Intent login = new Intent(mContext.getApplicationContext(), LoginActivity.class);
                login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(login);
                getActivity().finish();
            }
        } catch (Exception e) {
        }
    }
}

Stack trace of the error:

    android.view.InflateException: Binary XML file line #54: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
    at android.app.ActivityThread.access$700(ActivityThread.java:159)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:5419)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #54: Error inflating class fragment
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
    at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
    at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
    at com.xyz.activities.MainActivity.onCreate(MainActivity.java:232)
    at android.app.Activity.performCreate(Activity.java:5372)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
    ... 11 more
Caused by: java.lang.IllegalStateException: Fragment com.xyz.fragments.ActionsFragment did not create a view.
    at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2189)
    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
    at android.support.v7.app.ActionBarActivity.onCreateView(ActionBarActivity.java:547)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)

Similar errors on SO reference the fact that the xml fragment must have an id, which mine does, and that the super.OnCreate() should be called before setContentView() in the activity, which I do.

Any suggestions?

Edit:

fragment_action_bar_bottom.xml

<RelativeLayout
    android:id="@+id/action_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical">

    <ImageButton
        android:id="@+id/action_btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:background="@drawable/img_btn_1"/>

    <ImageButton
        android:id="@+id/action_btn_2"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:padding="8dp"
        android:background="@null"
        android:scaleType="centerInside"/>

</RelativeLayout>
Daniel Wilson
  • 18,838
  • 12
  • 85
  • 135
  • Where is the error? It's hard to see, for example. In logcat, "Binary XML file line #54: Error inflating", is that on line android.support.v7.widget.Toolbar? Another the logcat points to MainActivity.java:232, pls post it and tell us which one is line 232? – The Original Android Mar 28 '15 at 00:06
  • Line 232 is the setContentView() of that layout. Generally the root of errors are toward the bottom of the stack trace right? `java.lang.IllegalStateException: Fragment com.xyz.fragments.ActionsFragment did not create a view` is probably the relevant one. For some reason it cannot instantiate that fragment but only like 5% of the time! (I've never seen this crash occur myself which makes it hard to debug) – Daniel Wilson Mar 28 '15 at 00:14
  • So...is the error on ActionsFragment.java on inflate call? That is pointing to layout fragment_action_bar_bottom. Sorry rather confuse since I am trying to connect your posted files and the logcat. – The Original Android Mar 28 '15 at 00:27
  • No sorry for not being clearer, the xml posted is inflated in the MainActivity's onCreate() with setContentView() (line 232), where the app sometimes gives me that crash. The only info Logcat gives me is that the ActionsFragment 'did not create a view'. I don't know what condition causes this to be the case though – Daniel Wilson Mar 29 '15 at 00:26
  • I think relevant is layout fragment_action_bar_bottom.xml, pls post it too. I think so because of logcat text "Binary XML file line #54: Error inflating class fragment". And tell us which one is line 54, to avoid misunderstanding. This is probably going to be hard to debug since you said the problem is intermittent. – The Original Android Mar 29 '15 at 06:47
  • Okay added, it's just a simple relative layout with two buttons, I believe the issue relates to embedding this fragment in xml, I don't think it was a problem when it was added programmatically – Daniel Wilson Mar 29 '15 at 11:43
  • Did you post your whole onCreateView method? – natario Mar 29 '15 at 11:47
  • Okay that's basically the entire fragment added, including full onCreateView, nothing too fancy – Daniel Wilson Mar 29 '15 at 11:59
  • Ok, let's speed things up. Since "...Error inflating class fragment" is the possible suspect, I think we need to look at the MainActivity.java. Perhaps it's the timing of when it loads that fragment. – The Original Android Mar 29 '15 at 19:38

0 Answers0