0

I need to work back to API level 8. I have multiple activities and am using a navigation drawer to move between them as well as an action bar (with AppCompat v7). I have created a base abstract class that has all the navigation drawer setup and handling and that descends from ActionBarActivity.

File BaseNavActivity.java

abstract class BaseNavActivity extends ActionBarActivity {
}

All my activities subclass BaseNavActivity, and they all work fine.

public class MainActivity extends BaseNavActivity {
}

Before I added the navigation drawer feature I had a map working fine when descending from FragmentActivity. However, I cannot do that anymore because I need to descend from my BaseNavActivity. Even though the docs say ActionBarActivity descends from FragmentActivity I get an error when trying to use this XML.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background" >

    <!-- The main content view -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background"
        android:orientation="vertical"
        >

        <include layout="@layout/common_toolbar" />

        <fragment
            android:id="@+id/map"
            class="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="3dip"
            />

        <include layout="@layout/common_footer" />
    </LinearLayout> 
    <!-- The navigation drawer -->
    <include layout="@layout/nav_menu" />
</android.support.v4.widget.DrawerLayout>

I get an InflateException: Binary XML file line #19: Error inflating class fragment.

I have tried other solutions such as having a separate XML file with just the and opening it as a fragment and adding it to the above in place of the . That approach always returned a null when I tried to point to the map (smf was always null):

SupportMapFragment smf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

I also tried to have a

public class MapFragment extends FragmentActivity {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View myFragmentView = inflater.inflate(R.layout.location_map, container, false);

        return myFragmentView;
    }

}

but I couldn't add the fragment using

MapFragment mapFragment = new MapFragment();
fragmentTransaction.add(R.id.map_container, mapFragment);
fragmentTransaction.commit();

The add() function needed to have parameters of (int, Fragment).

Any help will be appreciated. If you need more code I can add it but didn't want to make this post too long. I have spent a couple of days searching and trying various approaches and nothing seems to get everything working together.

Basically I would like to know how to have a <fragment> inside the XML when descending from a custom subclass that descends from ActionBarActivity. My 2nd choice would be to know how to add a fragment that holds the map and works with the support libraries.

Thanks!

weston
  • 54,145
  • 21
  • 145
  • 203
Gravitoid
  • 1,294
  • 1
  • 20
  • 20

1 Answers1

0

I finally found my issue. I took a different approach for searching and found this gem: Android Google Maps V2 Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.Fragment

Basically my issue was that I had the custom base class call setContentView before calling the super.onCreate. That meant that fragments weren't understood because it was a couple of supers up that knew about them. This caught me because 1) I needed to specify a theme that appcompat could use and had my setContentView next to it when I copied things to the base class, and 2) everything else worked up to this point so I didn't realize it was incorrect.

So the fix is in my base class that all my activities inherit from (abbreviated):

abstract class BaseNavActivity extends ActionBarActivity {
    protected void onCreate(Bundle savedInstanceState, int resLayout) {

        // need to set the theme first
        setTheme(R.style.AppTheme2);

        // then call super before setting the content view
        super.onCreate(savedInstanceState);

        setContentView(resLayout);
    }
}
Community
  • 1
  • 1
Gravitoid
  • 1,294
  • 1
  • 20
  • 20