Sheharyar
Sheharyar

Reputation: 75840

"Error Inflating Class Fragment" on Android 4.2.x

I've been debugging for hours now and still no luck. I made a RSS Reader using Android fragments. It works amazingly well on devices running Android 4.0.x to 4.1.x but crashes on start on Android 4.2.x devices.

I'd really appreciate any help I can get.

Logcat:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.impsycho.androidpakistan/com.impsycho.androidpakistan.ItemListActivity}: 

android.view.InflateException: Binary XML file line #1: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5039)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
    at android.app.Activity.setContentView(Activity.java:1881)
    at com.impsycho.androidpakistan.ItemListActivity.onCreate(ItemListActivity.java:13)
    at android.app.Activity.performCreate(Activity.java:5104)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
Caused by: java.lang.NullPointerException
    at com.impsycho.androidpakistan.ItemListFragment$GetAllThePosts.onPreExecute(ItemListFragment.java:157)
    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
    at android.os.AsyncTask.execute(AsyncTask.java:534)
    at com.impsycho.androidpakistan.ItemListFragment.onCreate(ItemListFragment.java:54)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:835)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1160)
    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
    ... 20 more
Sending signal. PID: 30286 SIG: 9

Main Activity:

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class ItemListActivity extends FragmentActivity implements ItemListFragment.Callbacks {
    private boolean mTwoPane;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_list);
        getActionBar().setDisplayShowTitleEnabled(false);

        if (findViewById(R.id.item_detail_container) != null) {
            mTwoPane = true;
            ((ItemListFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.item_list))
                    .setActivateOnItemClick(true);
        }
    }
    ...

activity_item_list.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_list"
    android:name="com.impsycho.androidpakistan.ItemListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ItemListActivity" />

activity_item_twopane.xml

<LinearLayout 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:divider="?android:attr/dividerHorizontal"
    android:orientation="horizontal"
    android:showDividers="middle"
    tools:context=".ItemListActivity" >

    <fragment
        android:id="@+id/item_list"
        android:name="com.impsycho.androidpakistan.ItemListFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2.5" />

    <FrameLayout
        android:id="@+id/item_detail_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_weight="5" />

</LinearLayout>

Upvotes: 2

Views: 1472

Answers (1)

Sheharyar
Sheharyar

Reputation: 75840

Okay, so I figured out what was wrong.

I was focusing on the wrong error. The error was actually in my method GetAllThePosts() which is called when the app is started, because of which the layout could not be inflated. And it's not even an error because it works perfectly well in all other Android versions.

For some reason, you can't change the view of menu items while the app is starting in Android 4.2.x

The code responsible was:

private class GetAllThePosts extends AsyncTask<Void, Void, Void> {
        protected void onPreExecute() {
            RefreshMenuButton.setActionView(R.layout.action_progress);
            RefreshMenuButton.expandActionView();

            ...

I just added a conditional statement to check if we were on Android 4.2 and higher. If we are it won't run the first time.

Upvotes: 1

Related Questions