2013-12-18 64 views
1

需要你的幫助,點擊片段中的按鈕後ViewPager想要通過下一個頁面進行頁面I NullPointerException一個指向這段代碼的錯誤Fragment fragment = titleAdapter.getItem(position);我該如何解決它? 也許我做錯了什麼,但最後我想知道,當你點擊一個片段改變了另外兩個圖片和文本。NullPointerException當你滾動片段ViewPager

TitleAdapter.java:

public class TitleAdapter extends FragmentPagerAdapter { 
    public final String titles[] = new String[] { "A", "B", "C" }; 
    public final Fragment frags[] = new Fragment[titles.length]; 

    public TitleAdapter(FragmentManager fm) { 
     super(fm); 
     frags[0] = new FragmentA(); 
     frags[1] = new FragmentB(); 
     frags[2] = new FragmentC(); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     Log.v("TitleAdapter - getPageTitle=", titles[position]); 
     return titles[position]; 
    } 

    @Override 
    public Fragment getItem(int position) { 

     Log.v("TitleAdapter - getItem=", String.valueOf(position)); 
     return frags[position]; 
    } 

    @Override 
    public int getCount() { 
     return frags.length; 
    } 

} 

MainActivity.java:

public class MainActivity extends FragmentActivity { 
    boolean isPlayingA = false; 
boolean isPlayingB = false; 
boolean isPlayingC = false; 
    TitleAdapter titleAdapter; 
    ViewPager mViewPager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

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

     final TitleAdapter titleAdapter = new TitleAdapter(getSupportFragmentManager()); 
     mViewPager.setAdapter(titleAdapter); 
     mViewPager.setCurrentItem(1); 
     mViewPager.setOffscreenPageLimit(2); 
     } 

public void playA() { 

    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
     public void onPageSelected(int position) { 
      Fragment fragment = titleAdapter.getItem(position); 
      if (fragment instanceof FragmentB) { 
       ((FragmentB) fragment).bgStop(); 
      } 
        if (fragment instanceof FragmentC) { 
       ((FragmentC) fragment).bgStop(); 
      } 
      } 

     }); 
    }  
    isPlayingB = false; 
    isPlayingC = false; 

} 
} 

代碼是相同的,所述其它片段

FragmentA.java的:

public class FragmentA extends Fragment { 

    final static String LOG_TAG = "myLogs"; 
    static Button btnPlay; 


    public FragmentA() { 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_a, container, false); 

     btnPlay = (Button) rootView.findViewById(R.id.btnStart); 

     btnPlay.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View rootView) { 

      if (((MainActivity)getActivity()).isPlayingA) { 
        bgPlay(); 
       }else{ 
        bgStop(); 
        ((MainActivity)getActivity()).playA(); 

       } 
      ((MainActivity)getActivity()).isPlayingA = !((MainActivity)getActivity()).isPlayingA; 


      } 
      }); 


     return rootView; 
    } 





    public void bgPlay(){ 
       btnPlay.setText("Play"); 
      btnPlay.setBackgroundResource(R.drawable.play_button); 
    } 

    public void bgStop(){ 
       btnPlay.setText("Stop"); 
     btnPlay.setBackgroundResource(R.drawable.stop_button); 
    } 

}

+0

刪除這個'最後TitleAdapter'。 –

+0

非常感謝你! – user3064772

回答

0

在已創建的另一個變量爲TitleAdapter壓倒一切的全局(類)變量onCreate方法。

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

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

     //Here 
     final TitleAdapter titleAdapter = new TitleAdapter(getSupportFragmentManager()); 
     mViewPager.setAdapter(titleAdapter); 
     mViewPager.setCurrentItem(1); 
     mViewPager.setOffscreenPageLimit(2); 
     } 

而不是你的onCreate方法應該是

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

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

     titleAdapter = new TitleAdapter(getSupportFragmentManager()); 
     mViewPager.setAdapter(titleAdapter); 
     mViewPager.setCurrentItem(1); 
     mViewPager.setOffscreenPageLimit(2); 
     } 
+0

非常感謝你! – user3064772

+0

快樂幫助:)接受答案,如果它已經幫助你解決問題:) –