I am new in android i am stuck while working with fragment any help in this is really appreciated.I am working with fragment for tablet. There are two fragment suppose FragmentA(Left side fragment) and FragmentB(Right side Fragment) both are list fragment, FragmentB change according to FragmentA. There is another fragment, FragmentC which inflates on clicking the item on List of FragmentB. Now after inflating the FragmentC when I click on the FragmentA my app Crashes. There is an error in logcat showing FragmentC can't cast into FragmentB. I am trying to solve this problem from last 10 hours but no result came out.
This is my MainActivity class
public class MainActivity extends FragmentActivity implements FragmentA.Communicator{
FragmentManager manager;
FragmentB frag2;
FragmentA frag1;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frag);
manager=getSupportFragmentManager();
FragmentA frag1=(FragmentA ) manager.findFragmentById(R.id.fragment1);
frag1.setCommunicator(this);
}
@SuppressLint("NewApi")
@Override
public void respond(int index) {
// TODO Auto-generated method stub
frag2= (FragmentB) manager.findFragmentById(R.id.fragment2);
frag2.changeData(index);
}
}
This is my FragmentA class
public class FragmentA extends Fragment implements OnItemClickListener{
Communicator comm;
ListView list;
String[] items={"Beverages","Food"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_list,container, false);
list=(ListView) view.findViewById(R.id.listView1);
ArrayAdapter<String> adapter= new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,items);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
return view;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int i, long arg3) {
// TODO Auto-generated method stub
comm.respond(i);
}
public void setCommunicator(Communicator communicator){
this.comm=communicator;
}
public interface Communicator{
public void respond(int index);
}
}
This is my FragmentB class
public class FragmentB extends Fragment implements OnItemClickListener {
ListView listmenu;
String[] listItem1={"item1","item2","item3","item4","item5"};
String[] listItem2={"itemA","itemB","itemC","itemD","itemE"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu, container, false);
listmenu=(ListView) view.findViewById(R.id.listView_menu);
return view;
}
public void changeData(int index){
if(index==0){
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listItem1);
listmenu.setAdapter(adapter);
}
}
if(index==1){
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listItem2);
listmenu.setAdapter(adapter);
}
listmenu.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
//Intent intent = null;
switch(position){
case 0: FragmentManager manager=getFragmentManager();
Fragment frag1=new FragmentC();
FragmentTransaction ft=manager.beginTransaction();
ft.replace(R.id.fragment2, frag1,"A");
ft.addToBackStack("A");
ft.commit();
break;
}
}
}