0

I'm trying to implement a base activity with DrawerLayout so I can extend it to other activities.

I used the default Drawer Application in the Eclipse wizard and the help of these questions: Q1, Q2, and for some reason I get an inflate exception when I launch the app.

public class BaseActivity extends ActionBarActivity implements
    NavigationDrawerFragment.NavigationDrawerCallbacks {

/**
 * Fragment managing the behaviors, interactions and presentation of the
 * navigation drawer.
 */
private NavigationDrawerFragment mNavigationDrawerFragment;

/**
 * Used to store the last screen title. For use in
 * {@link #restoreActionBar()}.
 */
private CharSequence mTitle;

private DrawerLayout mDrawerLayout;
private FrameLayout mFrameLayout;

@Override
public void setContentView(int layoutResID) {
    mDrawerLayout = (DrawerLayout) getLayoutInflater().inflate(
            R.layout.activity_base, null);
    mFrameLayout = (FrameLayout) mDrawerLayout.findViewById(R.id.container);

    getLayoutInflater().inflate(layoutResID, mFrameLayout, true); /* Exception Here */

    super.setContentView(mDrawerLayout);

    mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
            .findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(R.id.navigation_drawer, mDrawerLayout);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_base);     
}

@Override
    public void onNavigationDrawerItemSelected(int position) {
    FragmentManager fragmentManager = getSupportFragmentManager();

    switch (position) {
    case 0:
        fragmentManager.beginTransaction()
                .replace(R.id.container, new AllCategoriesFragment()).commit();
        break;
    case 1:
        fragmentManager.beginTransaction()
                .replace(R.id.container, new AllRecipesFragment()).commit();
        break;
    }

}

activity_base.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.yonivy.cookpad.app.BaseActivity" >

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

<fragment
    android:id="@+id/navigation_drawer"
    android:name="com.yonivy.cookpad.app.NavigationDrawerFragment"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    tools:layout="@layout/fragment_navigation_drawer" />

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

AllCategoriesFragment.java

public class AllCategoriesFragment extends Fragment {
private ArrayList<Category> allCategories;
private CategoriesAdapter cAdapter;
private ListView lv;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.activity_all_categories, container, false);

    lv = (ListView) view.findViewById(R.id.mList);

    displayAllCategories();

    return view;
}

Sample extended class

public class RecipeActivity extends BaseActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recipe);
    // ...
}
Community
  • 1
  • 1
Yoni Levy
  • 1,562
  • 2
  • 16
  • 35
  • Well it is being used on its own, but i also tried it as an extension only and the problem repeats – Yoni Levy Oct 13 '14 at 03:23
  • I've added the layout and I'm doing exactly what you suggested in the extended classes. To deal with the double inflation I added a simple if statement `if(layoutResID != R.layout.activity_base)` but still no luck. – Yoni Levy Oct 13 '14 at 03:33
  • I don't know.. It's auto generated. I can check R.java but i doubt its the cause. – Yoni Levy Oct 13 '14 at 03:41
  • R.layout.activity_base but I'm not inflating it a second time, only when it's not the base class – Yoni Levy Oct 13 '14 at 03:46
  • One of several fragments. I'll add the code – Yoni Levy Oct 13 '14 at 03:48
  • I agree but it doesn't. Its navigation is identical to the other extended classes – Yoni Levy Oct 13 '14 at 03:55
  • Well i have an activity with tabs which are fragments themselves so i can't put fragment inside another. Anyway even if it's solvable by changing the architecture i still want to fix this issue or it will not stop bugging me. – Yoni Levy Oct 13 '14 at 04:05

0 Answers0