Monday, 16 February 2015

Android - Error inflecting class fragment



I have two fragments and one main activity having navigation drawer. In the navigation drawer I have two menu items i.e. Home and Places. I have created xml fragments for both menu items.


fragment_home.xml



<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
tools:context="com.gaurav.googlemap.HomeMap" >

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

</RelativeLayout>


fragment_places.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Places"
android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


Java Code



package com.beproject.ourway;

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;


public class MainHome extends FragmentActivity{
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
Fragment fragment = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_home);

// Initialization

mNavigationDrawerItemTitles= getResources().getStringArray(R.array.navigation_drawer_items_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);


// Defining drawer items
ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[2];

drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_launcher, "Home");
drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_launcher, "Places");


DrawerItemCustomAdapter adapter =
new DrawerItemCustomAdapter(this, R.layout.home_drawer_item, drawerItem);

mDrawerList.setAdapter(adapter);

mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

// selectItem(0);
}

public class DrawerItemClickListener implements ListView.OnItemClickListener {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}

}

void selectItem(int position) {



switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new PlacesFragment();
break;

default:
break;
}

if (fragment != null) {

mDrawerLayout.closeDrawer(mDrawerList);

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
getActionBar().setTitle(mNavigationDrawerItemTitles[position]);

} else {
Log.e("MainActivity", "Error in creating fragment");
}
}

public class HomeFragment extends Fragment {

public HomeFragment() {
}

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

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

return rootView;
}

}

public class PlacesFragment extends Fragment {

public PlacesFragment() {
}

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

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

return rootView;
}

}
}


Problem


When I run the project, I click on Home menu then it shows me home fragment with map in well manner. After that I click on Places then it shows me fragment of places fine. But when I again click on Home then my application is getting crashed with following exception



java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gaurav.googlemap/com.gaurav.googlemap.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment


Is there any correction in java file to open fragments appropriately?

Thanks


No comments:

Post a Comment