1

我正在创建一个应用程序,它有一个主要活动,其中有一个 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:在 ViewPager 中更改文本
如何更改片段内的视图?

访问页面、它们的视图和内容的正确方法是什么?

4

1 回答 1

0

在fragment类的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;
    }
于 2012-07-25T14:00:34.837 回答