I am a beginner in android programing. I want to write a program with fragment, i read this tutorial(http://www.mysamplecode.com/2012/08/android-fragment-example.html) and write that but when i run the program, the program has compiler errors!
The errors are about add() and replace() functions.
I write AndroidFragmentActivity class in below, Please read this and help me.
AndoridFragmentActivity.java:
package com.appfragmentarray;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import com.appfragmentarray.ListFragment.OnURLSelectedListener;
public class AndroidFragmentActivity extends Activity implements OnURLSelectedListener{
boolean detailPage = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("AndroidFragmentActivity", "onCreate()");
Log.v("AndroidFragmentsavedInstanceState", savedInstanceState == null ? "true" : "false");
setContentView(R.layout.activity_main);
if(savedInstanceState == null) {
android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
ListFragment listFragment = new ListFragment();
ft.add(R.id.displayList, listFragment, "List_Fragment");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
if(findViewById(R.id.displayDetail) != null){
detailPage = true;
getFragmentManager().popBackStack();
DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.displayDetail);
if(detailFragment == null){
android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
detailFragment = new DetailFragment();
ft.replace(R.id.displayDetail, detailFragment, "Detail_Fragment1");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}
}
public void onURLSelected(String URL) {
Log.v("AndroidFragmentActivity",URL);
if(detailPage){
DetailFragment detailFragment = (DetailFragment)
getFragmentManager().findFragmentById(R.id.displayDetail);
detailFragment.updateURLContent(URL);
}
else{
DetailFragment detailFragment = new DetailFragment();
detailFragment.setURLContent(URL);
android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.displayList, detailFragment, "Detail_Fragment2");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
}
}
The errors are:
Cannot cast from Fragment to DetailFragment
The method add(int, Fragment, String) in the type FragmentTransaction
is not applicable for the arguments (int, ListFragment, String)The method replace(int, Fragment, String) in the type
FragmentTransaction is not applicable for the arguments (int,
DetailFragment, String)
How can i solve this problems? Thanks.
You’re probably mixing up the compatibility Fragments and the normal 3.0+ Fragment classes.
If you want to use the compatibility package:
Make
AndroidFragmentActivityextendFragmentActivityChange
to
and make sure all calls to
getFragmentManager()are insteadgetSupportFragmentManager()Make sure your Fragments extend from
android.support.v4.app.Fragmentinstead ofandroid.app.FragmentOR
If you want to use the normal Fragments, get rid of all the
support.v4imports and make sure that your Fragments work withandroid.app.Fragment, which is the non-comptability Fragment.