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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:08:52+00:00 2026-05-29T11:08:52+00:00

In ViewPagerAdapter.class I have 2 layout(whereami.xml and mapview.xml), and use LayoutInflater to inflate each

  • 0

In ViewPagerAdapter.classI have 2 layout(whereami.xml and mapview.xml), and use LayoutInflater to inflate each view inside instantiateItem(), normally i should set everything(TextView,ImageView etc. according to xml) inside instantiateItem(). But in my case, i really need to setText or Background color at onCreate() of ViewPagerActivity which extend MapActivity

So, Are there anyway to set them in onCreate()?

P.S. I try to debug program and found that the process won’t call instantiateItem() until it get out of the onCreate() method. So,The View won’t create yet, and programe force close every time when i try to setText because View is Null.

ViewPagerActivity

public class ViewPagerActivity extends MapActivity {
    private ViewPagerAdapter adapter;
    private ViewPager pager;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        adapter = new ViewPagerAdapter(this);
        pager = (ViewPager) findViewById(R.id.viewpager);
        TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
        pager.setAdapter(adapter);
        indicator.setViewPager(pager);


        //LinearLayout bgcolor = (LinearLayout)findViewById(R.id.mainlayout);
        //bgcolor.setBackgroundColor(Color.Black);
        //TextView text1 = (TextView)findViewById(R.id.text1);
        //text1.setText("somevalue");

    }


}

ViewPagerAdapter

public class ViewPagerAdapter extends PagerAdapter implements TitleProvider {
    private final Context context;
    private MapView mapView;
    private MapController mapController;
    private View view;

    public ViewPagerAdapter(Context context) {
    }

    private static String[] titles = new String[] { "Page1", "Map" };

    @Override
    public Object instantiateItem(View pager, final int position) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (position == 0) {
            view = inflater.inflate(R.layout.whereami, null);
            ((ViewPager) pager).addView(view, 0);

        }
        if (position == 1) {
            view = inflater.inflate(R.layout.my_map_activity, null);
            ((ViewPager) pager).addView(view, 0);
            mapView = (MapView) view.findViewById(R.id.mapview);

            mapController = mapView.getController();
            mapController.setZoom(14);
        }

        return view;
    }

}

whereami.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<TextView
    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="8dp"
    android:textSize="16sp" >
</TextView>
</LinearLayout>
  • 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-29T11:08:53+00:00Added an answer on May 29, 2026 at 11:08 am

    If you know what should be in the TextView in onCreate() why not pass it through the constructor of the Adapter?

    new ViewPagerAdapter(this, "TextToPass");
    

    With:

    private String toDisplay;
    public ViewPagerAdapter(Context context, String toDisplay) {
        this.toDispay = toDisplay;
    }
    

    and:

    view = inflater.inflate(R.layout.whereami, null);
    TextView textView = view.findViewById(R.id.text1);
    textView.setText(toDisplay);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the ViewPager from the Android Support package (android.support.v4.view.ViewPager). My ViewPager layout
I am trying to use a PageAdapter. I found out that public Object instantiateItem(
I'm stuck here with a problem starting an activity from a Button inside a
I have a Timer in my onCreate(), it should set a new text when
I have this (noob?) question, but i just can't figure it out for many
I have a fragment interface with tabs along the bottom which open different fragments
I have a ViewPager with three items. I am trying to set the ViewPager
I would like to notify ViewPagerAdapter that dataset changed in Loader.onLoadFinish callback function. It
I have a fragment which contains a ViewPager. The ViewPager is associated with an
Please don't judge me, i'm extremely new on android developing... I wanna make an

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.