0

I try to start a tab, which can be swiped. It works fine! But if I try to implement Google Maps, then it works unit I switch the tab and go to the map back. I get these kind of Error:

E/AndroidRuntime(25324): FATAL EXCEPTION: main
E/AndroidRuntime(25324): android.view.InflateException: Binary XML file line #21: Error inflating class fragment

I searched many times in google and couldn't find a solution.

This is the MainActivity class Code:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager;

GoogleMapOptions nMap;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create the adapter that will return a fragment for each of the three primary sections
    // of the app.
    mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();

    // Specify that the Home/Up button should not be enabled, since there is no hierarchical
    // parent.
    actionBar.setHomeButtonEnabled(false);

    // Specify that we will be displaying tabs in the action bar.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Set up the ViewPager, attaching the adapter and setting up a listener for when the
    // user swipes between sections.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mAppSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            // When swiping between different app sections, select the corresponding tab.
            // We can also use ActionBar.Tab#select() to do this if we have a reference to the
            // Tab.
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by the adapter.
        // Also specify this Activity object, which implements the TabListener interface, as the
        // listener for when this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }

}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

    public AppSectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        switch (i) {
            case 0:
                // The first section of the app is the most interesting -- it offers
                // a launchpad into the other demonstrations in this example application.

                Fragment f1 = new LayersDemoActivity2();


                return f1;

            default:
                // The other sections of the app are dummy placeholders.
                Fragment fragment = new DummySectionFragment();
                Bundle args = new Bundle();
                args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
                fragment.setArguments(args);
                return fragment;
        }
    }

    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch(position){
        case 0: return "Timeline";
        case 1: return "Chat";
        case 2: return "Fh";
        case 3: return "Mehr";
        default: return "Section " + (position + 1);
        }
       }
      }


/**
 * A dummy fragment representing a section of the app, but that simply displays dummy text.
 */
public static class DummySectionFragment extends Fragment {

    public static final String ARG_SECTION_NUMBER = "section_number";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false);
        Bundle args = getArguments();
        ((TextView) rootView.findViewById(android.R.id.text1)).setText(
                getString(R.string.dummy_section_text, args.getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }
   }
}

Tab 1 calls this Fragment

public class LayersDemoActivity2 extends Fragment implements OnItemSelectedListener {


private GoogleMap mMap;

private CheckBox mTrafficCheckbox;
private CheckBox mMyLocationCheckbox;

//neu hinzugekommen
private LocationClient locationclient;
private String TAG = this.getClass().getSimpleName();
//neu hinzugekommen

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

    View rootView = inflater.inflate(R.layout.layers_demo, container, false);


    // Demonstration of a collection-browsing activity.
    rootView.findViewById(R.id.traffic)
            .setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    updateTraffic();
                }
            });

    // Demonstration of a collection-browsing activity.
    rootView.findViewById(R.id.my_location)
            .setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    updateMyLocation();
                }
            });

    // Demonstration of a collection-browsing activity.
    rootView.findViewById(R.id.button1)
            .setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if (!checkReady()) {
                        return;
                    }
                    try{
                        LatLng hier = new LatLng(mMap.getMyLocation().getLatitude(), mMap.getMyLocation().getLongitude());
                        Marker setzen = mMap.addMarker(new MarkerOptions().position(hier).title("Here I am!"));         
                    }catch(Exception e){

                    }                       


                }
            });        

    return rootView;
}


@Override
public void onStart() {
    super.onStart();

    Spinner spinner = (Spinner) getView().findViewById(R.id.layers_spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            getView().getContext(), R.array.layers_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);

    mTrafficCheckbox = (CheckBox) getView().findViewById(R.id.traffic);
    mMyLocationCheckbox = (CheckBox) getView().findViewById(R.id.my_location);


    setUpMapIfNeeded();
}

@Override
public void onResume() {
    super.onResume();
    setUpMapIfNeeded();
    if (mMap != null) {
        updateTraffic();
        updateMyLocation();
    }
}

private void setUpMapIfNeeded() {
    if (mMap == null) {
        mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
    }
}

private boolean checkReady() {
    if (mMap == null) {
        //getView().Toast.makeText(this, R.string.map_not_ready, Toast.LENGTH_SHORT).show();
        return false;
    }
    return true;
}



private void updateTraffic() {
    if (!checkReady()) {
        return;
    }
    mMap.setTrafficEnabled(mTrafficCheckbox.isChecked());
}



private void updateMyLocation() {
    if (!checkReady()) {
        return;
    }
    mMap.setMyLocationEnabled(mMyLocationCheckbox.isChecked());
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    setLayer((String) parent.getItemAtPosition(position));
}

private void setLayer(String layerName) {
    if (!checkReady()) {
        return;
    }
    if (layerName.equals(getString(R.string.normal))) {
        mMap.setMapType(MAP_TYPE_NORMAL);
    } else if (layerName.equals(getString(R.string.hybrid))) {
        mMap.setMapType(MAP_TYPE_HYBRID);
    } else if (layerName.equals(getString(R.string.satellite))) {
        mMap.setMapType(MAP_TYPE_SATELLITE);
    } else if (layerName.equals(getString(R.string.terrain))) {
        mMap.setMapType(MAP_TYPE_TERRAIN);
    } else {
        Log.i("LDA", "Error setting layer with name " + layerName);
    }
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
    // Do nothing.
    }

}

last but not least, this is layers_demo.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />
<!-- A set of test checkboxes. -->

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@id/map"
    android:background="#D000"
    android:orientation="vertical"
    android:padding="6dp" >

    <Spinner
        android:id="@+id/layers_spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <CheckBox
        android:id="@+id/traffic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onTrafficToggled"
        android:text="@string/traffic" />

    <CheckBox
        android:id="@+id/my_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onMyLocationToggled"
        android:text="@string/my_location" />
</LinearLayout>

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:onClick="tagOnMyLocation"
    android:text="@string/tag_me" />

and the activity_main.xml layout

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

I guess it has something to do with fragment, but I dont know how and I dont know why. Please help. :(

3
  • When you slide the viewpager do you have black background behide or overlapping the map ? Commented Sep 20, 2013 at 9:16
  • not really, it is like. Tab 1 with Map works fine, go to Tab 2, Works also fine. Tab 3 and 4 the same. But If I go back to Tab 1 with the map, a lot of Errors in LogCat. I can't see the map, the app will crash. Commented Sep 20, 2013 at 9:59
  • Try to change the mViewPager.setOffscreenPageLimit(your number of pages + 1); Commented Sep 20, 2013 at 10:18

1 Answer 1

1

Just paste this method after oncreatview method

@Override
public void onDestroyView() {

    Fragment f = (Fragment) getFragmentManager().findFragmentById(R.id.map);
    if (f != null) {
        getFragmentManager().beginTransaction().remove(f).commit();
    }

    super.onDestroyView();
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.