2013-12-17 61 views
0

需要幫助,試圖調用Activity的方法片段,我認爲這是不正確的請求標記片段。NullPointerException調用標記片段ViewPager

這麼問標籤:

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(); 
    fm.beginTransaction().add(frags[0], "fragA").commit(); 
    frags[1] = new FragmentB(); 
    fm.beginTransaction().add(frags[1], "fragB").commit(); 
    frags[2] = new FragmentC(); 
    fm.beginTransaction().add(frags[2], "fragC").commit(); 
} 



@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; 
} 

} 

感受到引起的onCreate(捆綁savedInstanceState)右

public class MainActivity extends FragmentActivity { 

TitleAdapter titleAdapter; 

ViewPager mViewPager; 

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

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

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


    FragmentManager fm = getSupportFragmentManager(); 



    FragmentA fragment = (FragmentA)fm.findFragmentByTag("fragA"); 
    fragment.bgStop(); 

這裏的方法本身

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); 




     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 titleAdapter參考。刪除其中的任何一個。 –

+0

你有關這兩條記錄? TitleAdapter titleAdapter = new TitleAdapter(getSupportFragmentManager());和FragmentManager fm = getSupportFragmentManager(); – user3064772

+0

主要活動後沒有。以及你得到NUll指針異常的地方。 –

回答

0

該代碼有太多問題。在這裏,讓我爲你解決這個問題。 以下是未經測試的,只需在我最喜歡的文本編輯器中編輯即可。

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); 
     // do not commit any fragments here, the adapter handles that. 
     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; 
    } 

} 

然後在活動:

public class MainActivity extends FragmentActivity { 
    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); 
     mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
      @Override 
      public void onPageSelected(int position) { 
       Fragment fragment = titleAdapter.getItem(position); 
       // Ideally use some sort of interface here. 
       if (fragment instanceof FragmentA) { 
        ((FragmentA) fragment).bgStop(); 
       } 
      } 
     }); 
    } 
} 
+0

它的工作,謝謝,但得到了下一個問題,我把它放在按下按鈕後點擊瀏覽片段後,下降錯誤java.lang.NullPointerException指向此代碼片段fragment = titleAdapter.getItem(position);我該如何解決它?或者你提供了「理想情況下在這裏使用某種接口。」怎麼做? – user3064772

+0

不太清楚你想在這裏實現什麼,但是我對接口的理解是你的片段應該「實現MyCommonFragmentInterface」,它是一個接口,它具有在三個接口之間通用的一些方法。 –