简体   繁体   English

Android findViewById在ViewPager中不起作用

[英]Android findViewById not working in ViewPager

New and working on ViewPager, and encounter some difficulties that would like you to offer some advice... 是新的并且正在使用ViewPager,并且遇到一些困难,希望您提供一些建议...

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 ViewPagerStyle1Activity类

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 App_Intro类

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 " 它在App_Intro类的findViewById App_Intro红色强调:“ 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? 实际上,如何将不同的活动放入ViewPager? Are there any examples? 有什么例子吗?

Many thanks in advance! 提前谢谢了!

使用以下内容:

 root.findViewById(..)  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM