0

I'm new to Android and find it brutal (there seems to be an near infinite number of details and dependencies to remember).

Anywho, I got the TextSwitcher1 example app working, which uses ViewSwitcher. I'm assuming ViewSwitcher is the way to go, need to either display a map or a table, user can pick, and switch back and forth.

So I created my MapActivity in another application, seems to work. Next integrate into main app. So, call View v = findViewById(R.layout.mapview); and then mSwitcher.addView(v); except "v" is null. Why? Do I create the activity? But I don't want to show it yet. Is there such a call as "create activity but hide it until needed"? Or am I barking up the wrong tree?

Thanks for any insight.

Gerry
  • 1,031
  • 1
  • 14
  • 30
  • Did the answer help resolve your issue or are you still having problems getting your ViewSwitcher to work? You can vote on the answer with the up/down arrows and mark an answer as "accepted" by clicking the checkbox if it helped you fix the problem! – Eric Brynsvold Sep 23 '11 at 04:53

1 Answers1

1

The findViewById function returns a View based on an ID resource (R.id.something) for whatever view you have loaded in your activity (using setContentView(R.layout.main)). In your sample code, you're using a layout resource (R.layout.mapview). You should inflate the XML file, which will return a View that you can use to add to the ViewSwitcher.

Example Code:

LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.mapview, null);
mSwitcher.addView(v);

However, you should be able to define everything in your XML file and not have to manually add the pages to your ViewSwitcher. Here's some example code on how to do that: http://inphamousdevelopment.wordpress.com/2010/10/11/using-a-viewswitcher-in-your-android-xml-layouts/

Eric Brynsvold
  • 2,880
  • 3
  • 17
  • 22