在FragmentPagerAdapter中引用/更新视图

时间:2012-06-11 12:21:30

标签: android android-viewpager android-view

我正在创建一个具有一个主要活动的应用程序,其中包含一个FragmentPagerAdapter。从定义XML创建并扩展了四个页面。当我尝试引用这些页面中包含的任何视图时,我得到一个NullPointerException。

以下是代码:

import com.viewpagerindicator.TitlePageIndicator;
import com.viewpagerindicator.TitlePageIndicator.IndicatorStyle;
import android.os.Bundle;  
import android.support.v4.app.FragmentActivity;  
import android.support.v4.view.ViewPager;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);  
        viewPager.setAdapter(myFragmentPagerAdapter);

        setCustomTitleBar(viewPager);

        **// NullPointerException in next line**
        TextView textView = (TextView) myFragmentPagerAdapter.getItem(0).getView().findViewById(R.id.page0_city);
    }

    private void setCustomTitleBar(ViewPager viewPager){
        TitlePageIndicator titleIndicator = (TitlePageIndicator) findViewById(R.id.page_indicator);
        titleIndicator.setViewPager(viewPager);
        float density = getResources().getDisplayMetrics().density;
        titleIndicator.setBackgroundColor(0x00000000);
        titleIndicator.setFooterColor(0xff99c224);
        titleIndicator.setFooterLineHeight(2 * density); //1dp
        titleIndicator.setFooterIndicatorHeight(3 * density); //3dp
        titleIndicator.setFooterIndicatorStyle(IndicatorStyle.Underline);
        titleIndicator.setTextColor(0xAAAAAAAA);
        titleIndicator.setTextSize(13 * density);
        titleIndicator.setSelectedColor(0xFFFFFFFF);
        titleIndicator.setSelectedBold(true);
    }
}

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

class MyFragmentPagerAdapter extends FragmentPagerAdapter {
    PageZero p0;
    PageOne p1;
    PageTwo p2;
    PageThree p3;

    public MyFragmentPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
        p0 = new PageZero();
        p1 = new PageOne();
        p2 = new PageTwo();
        p3 = new PageThree();
    }

    @Override
    public Fragment getItem(int index) {
        switch (index) {
        case 0:
            return p0;
        case 1:
            return p1;
        case 2:
            return p2;
        case 3:
            return p3;
        }
        return null;
    }

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

    public CharSequence getPageTitle(int position) {
        switch (position) {
        case 0:
            return "TODAY";
        case 1:
            return "MONDAY";
        case 2:
            return "TUESDAY";
        case 3:
            return "WEDNESDAY";
        }
        return "null";
    }
}

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PageZero extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.page0, container, false);
        return view;
    }
}

我确实找到了两个相关的问题,两个都没有给我带来任何结果:
Android: Change text within ViewPager
How do I change the view inside a fragment?

访问网页,视图和内容的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

在片段类的onCreateView方法中编写TextView textView =(TextView)findViewById(R.id.page0_city),类似这样 -

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.page0, container, false);
        TextView textView = (TextView)findViewById(R.id.page0_city);
        if(textView!=null) textView.setText("Page0");
        return view;
    }