> I was tinkering around with it and realized that just extending the
> Activity does not give the exception
> What I do is:
>
> 1> A class that extends Activity starts executing.
> 2> it instantiates another class. The instantiated class does not take
> any parameters in the constructor.
> 3> Inside the constructor it does a this.setContentView - this seems
> to be generating the exception, because if I remove this the program
> works.

Yeah, you really really really don't want to do that.

> Now I changed it a bit:
>
> The constructor takes an activity object. then inside the constructor,
> I do activity.setContentView . I don't think the onCreateOptionsMenu
> method is executing even though I am not getting any exception.

That's because while *you* think you have two activities, *Android*
doesn't. And, at the end of the day, what Android thinks is what counts.

I think you need to step back for a bit and determine why you feel you
want two activities, instead of one activity with two (or more) UI modes,
or one activity and a dialog, or something.

If you really do need two activities, then you need to use them *as
activities*, which is a bit more than just subclassing Activity. Read the
section in the Android documentation on Starting Activities and Getting
Results in:

http://code.google.com/android/reference/android/app/Activity.html

(or, um, there's two chapters on it in my book, see sig)

If you don't necessarily need them to be two activities, but you just have
two different UI modes you want to display, you can use ViewFlipper or
TabHost to toggle between the two modes from within one activity. You can
also try calling setContentView() repeatedly, though I haven't tried that
and would be a bit worried about possible side-effects.

So, for example, suppose we were writing an email client for Android.
Viewing the list of emails and viewing an individual email might need to
be separate activities, particularly if we are serving the emails
themselves from a ContentProvider and want other applications to be able
to have content:// Uri instances that can open upon our email viewer.

The email client also has a configuration activity, with more
configuration options than can reasonably fit in one screen. Here, we
don't necessarily need multiple activities. Rather than use a ScrollView
to navigate them all, we could use a TabHost. If we don't like TabHost
because the tabs are too fat in the M5 SDK, we could use ViewFlipper to
toggle between sections of the configuration UI, using buttons or an
options menu or something to let the user toggle back and forth.

So, make sure you want two activities, then use them the way Android
expects it; or, consider it one activity and change the UI as needed based
upon user input.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com
_The Busy Coder's Guide to Android Development_ -- Available Now!


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to