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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:08:34+00:00 2026-05-29T06:08:34+00:00

I have a fragment view that needs replacement with another fragment. When the ListFragment

  • 0

I have a fragment view that needs replacement with another fragment.

When the ListFragment item is selected, the DetailsFragment is to be replaced with another ListFragment by passing along Extras to the new ( or 2nd) ListFragment in the activity. My problem is that I am getting a “No Activity found to handle Intent {(has extras)}”. The ListFragment works fine when the activity first starts, but when i up date (replace) the Details activity with another ListFragment, I get the error.

This is my first Fragment activity and I guess I don’t know how to pass Extras properly between the fragment. I am most surely not using the fragment-manager/transaction classes properly(?). If anyone could correct my implementation, I would greatly appreciate it.

UPDATE: I added “i.setClass(getActivity(), ListFragment.class);” to the intent in the ListFragment class and now the Log error has changed to the following:

UPDTATE 2: I corrected my intents to Arguments as Devunwired sugested and it works just beautifully now. Thnx Devunwired. the onlyn issue I have now is that the backstack doesnt work when the back key is pressed. The corrected class is below:

LogCat (UPDATED):

     FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.andaero.test/com.andaero.test.fragments.ListFragment}: java.lang.ClassCastException: com.andaero.test.fragments.ListFragment cannot be cast to android.app.Activity
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1739)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
    at android.app.ActivityThread.access$500(ActivityThread.java:122)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:132)
    at android.app.ActivityThread.main(ActivityThread.java:4123)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassCastException: com.andaero.test.fragments.ListFragment cannot be cast to android.app.Activity

The ListFragment Class:

public class ListFragment extends android.app.ListFragment {

    boolean mDualPane;
    int mCurCheckPosition = 0;
    protected TextView activityTitle;

    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;

    String extStorageDirectory = Environment.getExternalStorageDirectory()
            .toString();
    File dbfile = new File(extStorageDirectory + "/Andaero/dB/Andaero.db");
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);

    private static final String QUERY_KEY = "QUERY_KEY";
    private static final String QUERY_ORDER = "QUERY_ORDER";

    private View layout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        layout = inflater.inflate(R.layout.listview, null);
        return layout;

    }

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

        Bundle extras = getActivity().getIntent().getExtras();
        Bundle arg = this.getArguments();//**ADDED TO GET THE ARGS

        /**
         * Get the query string from last activity and pass it to this
         * activity-----------------------------------------------------
         */
        String q = null;
        if (extras != null) {
            q = extras.getString(QUERY_KEY);
        }

        if (arg != null) {
        q = (String) (getArguments() != null ? getArguments().getString(
                "QUERY_KEY") : 1);
    }

        loadQuery(q);
    }

    public void loadQuery(String q) {

        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {

            String qO = getActivity().getIntent().getStringExtra("QUERY_ORDER");
            Cursor c = db.rawQuery(q + " ORDER BY `_id` " + qO, null);
            setListAdapter(new QueryAdapter(getActivity(), c));
            db.close();

        } else {
            Alerts.sdCardMissing(getActivity());
        }
    }

    public class QueryAdapter extends CursorAdapter {

        public QueryAdapter(Context context, Cursor c) {
            super(context, c);
            LayoutInflater.from(context);
        }

        @Override
        public void bindView(View v, Context context, final Cursor c) {

            int tvLabel = c.getColumnIndexOrThrow("label");
            String label = c.getString(tvLabel);
            final TextView labelTxt = (TextView) v.findViewById(R.id.label);

            if (labelTxt != null) {
                labelTxt.setText("(" + label + ")");
            }

            int tvTitle = c.getColumnIndexOrThrow("title");
            final String title = c.getString(tvTitle);
            TextView titleTxt = (TextView) v.findViewById(R.id.listTitle);

            if (titleTxt != null) {
                titleTxt.setText(title);
            }

            int tvDescription = c.getColumnIndexOrThrow("description");
            String description = c.getString(tvDescription);
            TextView descriptionTxt = (TextView) v.findViewById(R.id.caption);

            if (descriptionTxt != null) {
                descriptionTxt.setText(description);
            }

            int tvDate = c.getColumnIndexOrThrow("date");
            String date = c.getString(tvDate);
            TextView dateTxt = (TextView) v.findViewById(R.id.dateAdded);

            if (dateTxt != null) {
                dateTxt.setText(date);
            }

            int tvGoto = c.getColumnIndexOrThrow("gotoURL");
            final String gotoURL = c.getString(tvGoto);
            TextView gotoTxt = (TextView) v.findViewById(R.id.dummy);

            if (gotoTxt != null) {
                gotoTxt.setText(gotoURL);
            }

            gotoTxt.setVisibility(View.GONE);
            v.setTag(gotoURL);

            final ListView lv = getListView();
            lv.setEnabled(true);
            lv.setClickable(true);

            lv.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> arg0, View v, int arg2,
                        long arg3) {

                    // Create new fragment and transaction
                    Fragment newFragment = new ListFragment();
                    FragmentTransaction transaction = getFragmentManager()
                            .beginTransaction();

                    // Replace whatever is in the fragment_container view with
                    // this fragment,
                    // and add the transaction to the back stack
                    transaction.replace(R.id.detailFragment, newFragment);
                    transaction.addToBackStack(null);

                    String url = "";
                    url = (String) v.getTag();

                    int nI = c.getColumnIndexOrThrow("intent");
                    String intent = c.getString(nI);
                    Class<?> myIntent = null;
                    try {
                        myIntent = Class.forName("com.andaero.test.fragments"
                                + intent);
                    } catch (ClassNotFoundException e) {
                        Log.e("ERROR", "Class Not Found for new intent!");
                        e.printStackTrace();
                    }

                    int tvTitle = c.getColumnIndexOrThrow("title");
                    String title = c.getString(tvTitle);

                    int tvLabel = c.getColumnIndexOrThrow("label");
                    String label = c.getString(tvLabel);

                    String queryKey = "SELECT * FROM " + label;
                    c.close();
                    db.close();

                    Bundle args = new Bundle();//**REPLACED THE INTENTS
                   args.putString("QUERY_KEY", queryKey);
                   args.putString("KEY_URL", url);
                   args.putString("KEY_SUBTITLE", title);
                   args.putString("KEY_LABEL", label);
                   args.putString("KEY_INTENT", intent);
                   args.putString("QUERY_ORDER", "ASC");

                   newFragment.setArguments(args);

                    // Commit the transaction
                    transaction.commit();
                }
            });
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            final View v = LayoutInflater.from(context).inflate(
                    R.layout.list_item, parent, false);
            return v;
        }
    }
}
  • 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-05-29T06:08:34+00:00Added an answer on May 29, 2026 at 6:08 am

    You’re on the right track with using the FragmentTransaction to replace one Fragment with another, but Fragment doesn’t use an Intent to pass data from one instance to another like Activity does. In fact, calling startActivity() with an Intent pointed at a Fragment will cause all kinds of fireworks that you don’t want (like you’ve seen).

    It is perfectly okay to pass data to a new Fragment in its constructor, so you can create a constructor for your ListFragment that takes any parameters of data you want to forward. Another option is to set all of your “extras” as arguments on the new Fragment by putting them in a Bundle and using Fragment.setArguments(). Any time you want to access the arguments you attached to the Fragment, you can just call getArguments() to get back that same Bundle. So basically, replace all the code having to do with an Intent in your onItemClick() method, and instead:

    Bundle args = new Bundle();
    args.putString("QUERY_KEY", queryKey);
    //...add all the extras to the bundle
    
    newFragment.setArguments(args);
    
    transaction.commit();
    

    Also, off-topic, but you might want to rename your Fragment to something else so that you don’t have to rely on the fully qualified package name to tell the difference between your ListFragment and the platform’s version in your code.

    HTH

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

Sidebar

Related Questions

I have a custom Fragment that inflates it's content from test.xml @Override public View
I have a simple little code fragment that is frustrating me: HashSet<long> groupUIDs =
I need to view QGraphicsScene in 2 QGraphicsViews with condition that they have different
Problem: I have a custom Calendar view that I generated using canvas drawing and
I'm working on a project that needs Jasper reporting, I have used the code
I currently have an OpenGL view that takes up nearly my entire screen. On
I have a reusable HTML fragment that I use to list items. So to
I have a fragment in an activity that I am using as a navigation
In an Android application I have a fragment implemented that overrides onViewCreated to set
I have a fragment that I need to display on the screen. I want

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.