2017-03-05 60 views
0

工作我也聲明3片段頁面查看瀏覽量。我的應用程序沒有響應,只要我跑了沒有錯誤。我引用這個YouTube視頻 https://www.youtube.com/watch?v=NYjgX-GNRGo。我認爲這段代碼已經過時了。有人能告訴我,我該如何糾正這一點?滾動PagerView沒有在Android Studio中

包com.example.name.scrollable;

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.view.View; 

    public class MainActivity extends FragmentActivity 
    {  
     ViewPager mViewPager = null; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) 
    {  
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mViewPager = (ViewPager) mViewPager.findViewById(R.id.pager); 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     mViewPager.setAdapter(new MyFragmentAdapter(fragmentManager)); 
    }  
    } 

class MyFragmentAdapter extends FragmentPagerAdapter 
{  
    public MyFragmentAdapter(FragmentManager fm) 
    { 
     super(fm);  
    } 

    @Override 
    public Fragment getItem(int position) 
    { 
     Fragment fragment = null; 

     if(position == 0) 
     { 
      fragment = new Fragment_a(); 
     } 

     if(position == 1) 
     { 
      fragment = new Fragment_b(); 
     } 

     if (position == 2) 
     { 
      fragment = new Fragment_c(); 
     } 
     return fragment; 
    } 

     @Override 
    public int getCount() 
    { 
     return 3; 
    } 
} 

我還讓3片段頁面顯示我可以ViewPager的幫助顯示

回答

0

添加這樣一條:

mViewPager = (ViewPager)findViewById(R.id.pager); 

,而不是這樣的:

mViewPager = (ViewPager) mViewPager.findViewById(R.id.pager);