findViewById始终使用FragmentStatePagerAdapter返回null

时间:2015-12-15 16:59:13

标签: android android-fragments android-viewpager findviewbyid

我正在尝试更新某些片段中放置在ViewPager中的一些视图, 每次我使用findViewById时,我都会返回null。 我尝试了我在这里找到的各种解决方案(堆栈溢出)并没有任何帮助。

这里是所有代码:

activity_main.xml中

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

MainActivity.java:

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

    _viewPager = (ViewPager) findViewById(R.id.viewpager);
    _pageManager = new PageManager(getSupportFragmentManager());
    _viewPager.setAdapter(_pageManager);
    _imageView=(ImageView)findViewById(R.id.PictureBox);

PageManager.java

public class PageManager extends FragmentStatePagerAdapter {

    final int NUMBER_OF_PAGES = 3;

    public PageManager(android.support.v4.app.FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new CountdownPage();
            case 1:
                return new VideosPage();
            case 2:
                return new SeasonSummary();
            default:
                return null;
        }
    }

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

CountdownPage.java

public class CountdownPage extends Fragment {
    int resId=0;
    ImageView _imageView;
    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {

        if (container==null)
            ShowMessage.GetInstance(null).ShowMessage("error","container");

       return  inflater.inflate(R.layout.countdown_page,container,false);
    }


}

countdown_page.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:background="@color/Black"
          android:layout_width="match_parent"
          android:id="@+id/countdownLayout"
          android:layout_height="match_parent">


<TextView
        style="@style/HeaderStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Countdown"
        android:gravity="center"
        android:id="@+id/CountdownHeader"
/>

<ImageView
        android:layout_width="320dp"
        android:layout_height="320dp"
        android:id="@+id/PictureBox"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:onClick="testF"
/>

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:id="@+id/counterTextView"
        android:gravity="center"
        android:textColor="#FFFF"
/>


</LinearLayout>

我试图解决这个问题好几个小时 非常感谢, 或Yaacov

1 个答案:

答案 0 :(得分:0)

你的ImageView(id是PictureBox)位于&#39; countdown_page&#39; layout xml not&#39; activity_main&#39; layout xml,所以你不应该在&#39; onCreate()&#39;中通过findViewbyId(int)获取ImageView。 Activity中的方法,ImageView属于CountdownPage片段。