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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:30:10+00:00 2026-06-16T02:30:10+00:00

I have a fragment which contains only a GridView and I have an adapter

  • 0

I have a fragment which contains only a GridView and I have an adapter that uses LayoutInflater to create its views from XML. Below is my fragment:

public class MagazineFragment extends SherlockFragment{
private GridView mGridView;

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

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mGridView = (GridView)view.findViewById(R.id.grid_view);
    mGridView.setAdapter(new MagazineAdapter(getActivity(), savedInstanceState));
}

class MagazineAdapter extends BaseAdapter{
    int[] mImages;
    private Context mContext;
    private Bundle mSavedInstanceState;

    public MagazineAdapter(Context context, Bundle savedInstanceState) {
        mContext = context;
        mSavedInstanceState = savedInstanceState;
        mImages = getResources().getIntArray(R.array.magazines);
    }

    @Override
    public int getCount() {
        return mImages.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ImageView imageView;
        if(convertView == null){
            imageView = (ImageView)getLayoutInflater(mSavedInstanceState).inflate(R.layout.magazine_grid_item, parent, false);
        }else{
            imageView = (ImageView)convertView;
        }

        imageView.setImageResource(mImages[position]);
        return imageView;
    }

}

}

and magazine_grid.xml:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:columnWidth="150dip"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:stretchMode="columnWidth"
    android:scrollbarStyle="outsideOverlay"
    android:padding="4dip"/>

and magazine_grid_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="150dip"
    android:layout_height="200dip"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:paddingTop="6dip"
    android:paddingBottom="6dip" 
    android:paddingLeft="2dip"
    android:paddingRight="2dip"/>

log message:

12-19 08:38:36.675: E/AndroidRuntime(11977): FATAL EXCEPTION: main 12-19 08:38:36.675: E/AndroidRuntime(11977): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.coderdem.android.zaytung/com.coderdem.android.zaytung.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 12-19 08:38:36.675: E/AndroidRuntime(11977):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967) 12-19 08:38:36.675: E/AndroidRuntime(11977):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 12-19 08:38:36.675: E/AndroidRuntime(11977):   at android.app.ActivityThread.access$600(ActivityThread.java:127) 12-19 08:38:36.675: E/AndroidRuntime(11977):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 12-19 08:38:36.675: E/AndroidRuntime(11977):    at android.os.Handler.dispatchMessage(Handler.java:99) 12-19 08:38:36.675: E/AndroidRuntime(11977):     at android.os.Looper.loop(Looper.java:137) 12-19 08:38:36.675: E/AndroidRuntime(11977):     at android.app.ActivityThread.main(ActivityThread.java:4511) 12-19 08:38:36.675: E/AndroidRuntime(11977):   at java.lang.reflect.Method.invokeNative(Native Method) 12-19 08:38:36.675: E/AndroidRuntime(11977):    at java.lang.reflect.Method.invoke(Method.java:511) 12-19 08:38:36.675: E/AndroidRuntime(11977):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980) 12-19 08:38:36.675: E/AndroidRuntime(11977):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) 12-19 08:38:36.675: E/AndroidRuntime(11977):    at dalvik.system.NativeStart.main(Native Method) 12-19 08:38:36.675: E/AndroidRuntime(11977): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 12-19 08:38:36.675: E/AndroidRuntime(11977):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3342) 12-19 08:38:36.675: E/AndroidRuntime(11977):    at android.view.ViewGroup.addView(ViewGroup.java:3213) 12-19 08:38:36.675: E/AndroidRuntime(11977):     at android.view.ViewGroup.addView(ViewGroup.java:3170) 12-19 08:38:36.675: E/AndroidRuntime(11977):     at android.view.ViewGroup.addView(ViewGroup.java:3150) 12-19 08:38:36.675: E/AndroidRuntime(11977):     at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40) 12-19 08:38:36.675: E/AndroidRuntime(11977):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915) 12-19 08:38:36.675: E/AndroidRuntime(11977):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) 12-19 08:38:36.675: E/AndroidRuntime(11977):   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 12-19 08:38:36.675: E/AndroidRuntime(11977):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) 12-19 08:38:36.675: E/AndroidRuntime(11977):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551) 12-19 08:38:36.675: E/AndroidRuntime(11977):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1136) 12-19 08:38:36.675: E/AndroidRuntime(11977):  at android.app.Activity.performStart(Activity.java:4480) 12-19 08:38:36.675: E/AndroidRuntime(11977):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1940) 12-19 08:38:36.675: E/AndroidRuntime(11977):  ... 11 more

What I am doing wrong?

  • 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-16T02:30:12+00:00Added an answer on June 16, 2026 at 2:30 am

    Your onCreateView:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // why?
        container.removeAllViews();
        return inflater.inflate(R.layout.magazine_grid, container);
    }
    

    is incorrect. Try to modify it like this:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
         // Android will attach the view returned by this method on its own so don't 
         // add it yourself to the container
         return inflater.inflate(R.layout.magazine_grid, container, false);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view pager which contains a fragment. When the fragment pager adapter
I have a fragment which has its own state (selected buttons, etc). That state
I have a HTML fragment which contains two anchor tags in various parts of
I have a xsl:variable ($features-list) specified in my XSL which contains a tree fragment
I have an Activity which uses a Fragment . I simply want to pass
I have a Fragment that has contains lots of info (article) with a tabhost
I have a fragment which pulls down data from a webservice and displays this
I have made a simple fragment of html, which contains this: <a href=#><div>Something here</div></a>
I have a product management page that contains a textarea in which some html
I have a ViewPager within a Fragment which contains three Fragments we'll call V1,

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.