pearmak
pearmak

Reputation: 5027

Android findViewById not working in ViewPager

New and working on ViewPager, and encounter some difficulties that would like you to offer some advice...

ViewPagerAdapter class

public class ViewPagerAdapter extends FragmentPagerAdapter 
{
    private Context _context;

    public ViewPagerAdapter(Context context, FragmentManager fm) 
    {
        super(fm);  
        _context=context;       
    }

    @Override
    public Fragment getItem(int position) 
    {
        Fragment f = new Fragment();
        switch(position){
        case 0:
            f=App_Intro.newInstance(_context);  
            break;
        case 1:
            f=LayoutTwo.newInstance(_context);  
            break;
        }
        return f;
    }
    @Override
    public int getCount() 
    {
        return 2;
    }
}

ViewPagerStyle1Activity class

public class ViewPagerStyle1Activity extends FragmentActivity 
{
    private ViewPager _mViewPager;
    private ViewPagerAdapter _adapter;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setUpView();
        setTab();
    }

    private void setUpView()
    {       
         _mViewPager = (ViewPager) findViewById(R.id.viewPager);
         _adapter = new ViewPagerAdapter(getApplicationContext(),getSupportFragmentManager());
         _mViewPager.setAdapter(_adapter);
         _mViewPager.setCurrentItem(0); 
    }
    private void setTab()
    {
        _mViewPager.setOnPageChangeListener(new OnPageChangeListener()
        {                       
            @Override
            public void onPageScrollStateChanged(int position) {}
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {}
            @Override
            public void onPageSelected(int position) 
            {
                // TODO Auto-generated method stub
                switch(position)
                {
                    case 0:
                        findViewById(R.id.first_tab).setVisibility(View.VISIBLE);
                        findViewById(R.id.second_tab).setVisibility(View.INVISIBLE);
                        break;

                    case 1:
                        findViewById(R.id.first_tab).setVisibility(View.INVISIBLE);
                        findViewById(R.id.second_tab).setVisibility(View.VISIBLE);
                        break;
                }
            }                       
        });
    }
}

App_Intro class

public class App_Intro extends Fragment 
{
    public static Fragment newInstance(Context context) 
    {
        App_Intro f = new App_Intro();          
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.app_introduce_scroll, null);

          Button calpaidBtn = (Button) findViewById(R.id.calpaidBtn); //RED UNDERLINE ERROR
          calpaidBtn.setOnClickListener(new OnClickListener() 
          {
              public void onClick(View v) 
              {  
                   Intent intent = new Intent (Intent.ACTION_VIEW, 
                           Uri.parse("https://abc.com"));
                   startActivity(intent);                                      
                   return;  
               }  
           });          
        return root;
    }

}

Question:

It underlines red in the App_Intro class for findViewById that "The method findViewById(int) is undefined for the type App_Intro"

How does this be solved? Actually how to put different activities into the ViewPager? Are there any examples?

Many thanks in advance!

Upvotes: 0

Views: 3200

Answers (1)

Plato
Plato

Reputation: 2348

Use the following :

 root.findViewById(..)  

Upvotes: 1

Related Questions