Android - Error inflecting class fragment



I am making Navigation Drawer. In that when I click on first menu item(name is "Home"), I have written code to open google map within fragment. When I click on second item(name is "Places"), I just displayed a TextView. Actually I have 6 menu items in the drawer, but now I am working on only 1st two items. When I run the main activity and swipe from left then navigation drawer opens fine. When I click on 1st item(Home) then fragment loads fine and it displays google map as I want. After that when I click 2nd item(Places), TextView opens fine. But actual problem is when I click again, my application crashes and throws an 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


Here is code for all files :


src/package/MainHome.java



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[6];

drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_launcher, "Home");
drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_launcher, "Places");
drawerItem[2] = new ObjectDrawerItem(R.drawable.ic_launcher, "Friends");
drawerItem[3] = new ObjectDrawerItem(R.drawable.ic_launcher, "Settings");
drawerItem[4] = new ObjectDrawerItem(R.drawable.ic_launcher, "Help");
drawerItem[5] = new ObjectDrawerItem(R.drawable.ic_launcher, "About");

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;
}

}
}


res/layout/main_home.xml



<android.support.v4.widget.DrawerLayout xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>


src/package/DrawerItemCustomAdapter.java



package com.beproject.ourway;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class DrawerItemCustomAdapter extends ArrayAdapter<ObjectDrawerItem> {

Context mContext;
int layoutResourceId;
ObjectDrawerItem data[] = null;

public DrawerItemCustomAdapter(Context mContext, int layoutResourceId, ObjectDrawerItem[] data) {

super(mContext, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View listItem = convertView;

LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
listItem = inflater.inflate(layoutResourceId, parent, false);

ImageView imageViewIcon = (ImageView) listItem.findViewById(R.id.imageViewIcon);
TextView textViewName = (TextView) listItem.findViewById(R.id.textViewName);

ObjectDrawerItem folder = data[position];


imageViewIcon.setImageResource(folder.icon);
textViewName.setText(folder.name);

return listItem;
}
}


res/layout/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>


res/layout/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>


res/values/strings.xml



<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="navigation_drawer_items_array">
<item>Home</item>
<item>Places</item>
<item>Friends</item>
<item>Settings</item>
<item>Help</item>
<item>About</item>
</string-array>
</resources>


Thanks.


No comments:

Post a Comment