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 8053299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:47:56+00:00 2026-06-05T07:47:56+00:00

I try to simply replace one custom fragment by another one. The first custom

  • 0

I try to simply replace one custom fragment by another one. The first custom fragment is called MenuFragment and extends ListFragment. When the activity is created (onCreate) I insert it in a layout (called layout_container) I’ve defined in XML :

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
MenuFragment menufragment = new MenuFragment(); 
fragmentTransaction.add(R.id.layout_container, menufragment);
fragmentTransaction.commit();

Here, I’ve got no problem.

Then I want to replace it with the second fragment called AlbumsFragment (which also extends ListFragment) when the user clicks on something. In my onClick event I put :

AlbumsFragment albumsfragment = new AlbumsFragment(); 
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.layout_container, albumsfragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

Here the new fragment is well displayed and replaces the menufragment but at the same time the app crashes and the LogCat says :

 E/AndroidRuntime(25105): java.lang.ClassCastException: com.music.musicapp.AlbumsFragment cannot be cast to com.music.musicapp.MenuFragment

I’m only using and targeting Api level 15 and so I don’t use the Android Support Package.

EDIT :
Here is my Album fragment code, very simple, it is just a list with two items “Album1” and “Album2”. I can see these two items after the click is performed, but immediatly the app crashes.

package com.music.musicapp;

import java.util.ArrayList;

import android.app.ListFragment;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class AlbumsFragment extends ListFragment {

            // declare the array list
             ArrayList<String> array = new ArrayList<String>();

             @Override
                public void onActivityCreated(Bundle savedInstanceState) {
                    super.onActivityCreated(savedInstanceState);


                // Fill up Array 
                    array.add("album1");
                    array.add("album2");

                    setListAdapter(new ArrayAdapter<String>(getActivity(),
                            android.R.layout.simple_list_item_1, array));


                 }   

}
  • 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-05T07:47:59+00:00Added an answer on June 5, 2026 at 7:47 am

    Problem solved,

    I had another method which needed the MenuFragment and thus, when this fragment was replaced by AlbumsFragment, this method was making the app crash because it refered to a no more existent fragment.

    So the problem was not in the code posted, sorry. It is important to consider the impact of replacing a fragment, and be careful when referring to it (check if it is still on the UI or if it has been already sent to the back stack, etc…).

    Thanks for your answers and sorry for not having seen that my problem wasn’t there (but when the project’s getting complex, is hard to always see from where the problem comes :/).

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

Sidebar

Related Questions

I know, that I am not the first one to try to use Cocoa
I simply try to assign a background color to each div. it looks simple
I'm going to try and explain this as simply as I can, it's most
I'm loading a comment list and try to replace the list I currently have
I would like to replace a image with another image when a link is
All, I've just started using Python (v 2.7.1) and one of my first programs
First I try to explain the circumstances. I store the the filter expression in
I'm interested in finding out what's the shortest script one can write to replace
I thought I'd try a simple GUI app using the world/universe mutation-free approach, but
in simple I try do that in PhpMyAdmin: update wp_postmeta.meta_value = wp_posts.id where wp_posts.post_name=articles.image

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.