Frage

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.

War es hilfreich?

Lösung

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

Andere Tipps

Inflating means parsing an XML description of an object and building a Java object with the described attributes. Activities do not have a detailed description in XML and thus no point to inflate them.

You can dispatch activity A from activity B or you can use the new concept of Fragment to combine multiple sub activities into activities.

BTW, you can define custom views (by subclassing one of the view classes) and use it in your XML layouts, just put the class full path e.g. in the layout instead of let say .

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top