Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9214897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:04:31+00:00 2026-06-18T02:04:31+00:00

I am a beginner in android programing. I want to write a program with

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:04:32+00:00Added an answer on June 18, 2026 at 2:04 am

    You’re probably mixing up the compatibility Fragments and the normal 3.0+ Fragment classes.

    If you want to use the compatibility package:

    Make AndroidFragmentActivity extend FragmentActivity

    Change

    android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
    

    to

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    

    and make sure all calls to getFragmentManager() are instead getSupportFragmentManager()

    Make sure your Fragments extend from android.support.v4.app.Fragment instead of android.app.Fragment

    OR

    If you want to use the normal Fragments, get rid of all the support.v4 imports and make sure that your Fragments work with android.app.Fragment, which is the non-comptability Fragment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

the tutorial here http://www.edumobile.org/android/android-beginner-tutorials/creating-image-gallery/ teaches how to put images from resources in gallery here's
I am beginner in android programming. I want to learn Fragment in android and
I'm beginner in Android (and java), and I'm just playing around and want to
I'm just a beginner with android. I want to divide a bitmap image into
I am a beginner in Android programming. I want to build a simple application
I'm a beginner in Android development but not in programming itself. Anyway, this question
I am a beginner in Android programming and would want to display text entered
I have written an app for the android. I want to make this ad
I am a beginner in Android programming. I was going through the official tutorial
Beginner Android and Java developer here. I have this snippet of code inside a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.