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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:12:15+00:00 2026-05-28T02:12:15+00:00

I don’t seem to understand if it’s possible to inflate (include) an activity into

  • 0

I don’t seem to understand if it’s possible to inflate (include) an activity into another activity. I know i can inflate a layout xml, this works, but i am wondering if i can inflate an activity. For instance , i have class A that extends Activity and another class B that extends ListActivity. Can i include and use in class A, my class B?
THis is what i have tried:

Class A:

LayoutInflater inflater = (LayoutInflater) MyActivity.this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // inflate list
    BActivity list = new BActivity();

Class B:

public class BActivity extends ListActivity {
  public void onCreate(Bundle bundle) {
    super.onCreate(bundle);

    List<Model> models= new ArrayList<Model>();
    models.add(new Model("John"));
    models.add(new Model("Cage"));

    setListAdapter(new MyAdapter(this, models));
    ListView list = getListView();
  }
}

and in xml (the class A xml): (for where i want to see the list)

 <view class="com.test.BActivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >  </view>

All of this throws errors :

Error inflating class BActivity

The activities are declared in the manifest.

Do you know what i am doing wrong? this is not the correct way to inflate another activity? I am using Android 2.2 api 8.
Thank you for your time.

  • 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-28T02:12:15+00:00Added an answer on May 28, 2026 at 2:12 am

    Your question title and your issue are not actually the same thing. For completeness, I will answer both.

    What is the difference from inflating an activity and inflating a view in android?

    The answer is there is no difference. Ultimately, they are the same in process and logic. However, an Activity may have many different Views and you may setContentView() several times to several different Layouts or Views based on your need. An Activity requires a Layout resource, and a View may or may not be a Layout.

    Do you know what i am doing wrong?

    Yes. Absolutely.

    1. Your code: BActivity list = new BActivity(); is not actually inflating an Activity. You are constructing the Activity, but not starting it.
    2. Your XML defines BActivity as a View, but your code defines it as an ListActivity. These are two different things entirely. A ListActivity has a ListView (extended or otherwise); A ListActivity is not a ListView.
    3. Activity and its subclasses are Contexts that have a Life Cycle that is managed by the OS. They contain and speak to Views of all types, but are not themselves Views.

    this is not the correct way to inflate another activity?

    No sir, but fear not! The answer is not too far away.

    1. FAKE ANSWER (for completeness) –
      First, to start another Activity so that it is inflated, you must call startActivity() from a Context. A Context may be an Application, Activity, Broadcast Reciever or any other app component (Component = declared object in your Android project manifest). So, if you really wanted to start a new Activity, you would change BActivity list = new BActivity(); to:

      Intent _listActivity = new Intent();
      _listActivity.setClass(BActivity.class);
      startActivity(_listActivity);
      
    2. REAL ANSWER –
      However, since you want to see your List in class A, BActivity is not an Activity, it is a View. That means what you REALLY want is to make it recognize your View and this is a different solution. Change public class BActivity extends ListActivity to public class BActivity extends ListView and now all of a sudden you have a custom View!! Now all we have to do is get the List to work.

    3. Constructing the View – Views are different from Activities in that they do not have a public void onCreate(Bundle bundle). All of your stuff from BActivity.onCreate() would instead be placed in the constructor. But, you don’t have a proper constructor… hmmm. Well, there are three constructors to choose from — add one or all of the following (You will probably want either option 1 or 2, at first. But you won’t use both at the same time hint hint, read the comments:

      //This constructor is used when the View is created from code (not XML!!)
      public BActivity(Context context)
      {
      }
      
      //This constructor is used when the View is created from XML (not code!!)
      public BActivity(Context context, AttributeSet attr)
      {
      }
      
      //This constructor is used when the View is created from XML with a Style defined in separate XML.
      public BActivity(Context context, AttributeSet attr, int defStyle)
      {
      }
      
    4. Inflating the Activity = Inflating the View
      You have a choice here, you can either add the View, or you can inflate the View. There are many options for both. Based on your question, I shall assume you want to inflate the View. Simply change BActivity list = new BActivity(); to setContentView(R.id.MyXML). MyXML, of course, would be the name of your XML Layout file. SetContentView will then open the appropriate View for you (BActivity) using the 2nd constructor from the list above.

    Understanding the difference between View and Activities is important. The processes between them are very similar, but they themselves have a intertwined but separate purpose.

    • An Activity MUST have a View.
    • A View MUST be in a Context.
    • An Activity is a Context, but a Context may also be one of several other possible classes.
    • Both may be inflated using a LayoutInflater
    • An Activity has a convenience method called setContentView which can inflate an entire XML file.
    • A View must inflate each View manually using LayoutInflater object.inflate().
    • An Activity has a Life Cycle. A View has a draw cycle instead.

    For more information, certainly read more on the Android Developers Resources. However, some of these things are only learned by experimentation.

    Hope this all helped!

    FuzzicalLogic

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

Sidebar

Related Questions

Don't know if anyone can help me with this or if it's even possible.
Don't know if this has been asked before, so point me to another question
don't know if this is possible.. I'm using sqlite3 schema: CREATE TABLE docs (id
Don't know why but I can't find a solution to this. I have 3
I don't understand where the extra bits are coming from in this article about
(Don't know if this is strictly on-topic, but I don't see any better Stack
I don't know: if this works. if it's a good idea. what it is
I don't know if this question is trivial or not. But after a couple
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't know if this is the right place to ask this, but I will

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.