实际上我正在使用Fragments和FragmentPagerAdapter,但是当我试图在片段中再次访问视图时,我遇到的问题是null。 pageNum也是null。我尝试了很多方法来解决这个问题,但它并没有起作用。
FirstFragment.java
public class FirstFragment extends Fragment{
private int pageNum;
private TextView titleTxt;
private LinearLayout topPan;
public static FirstFragment newInstance(int page) {
FirstFragment fragment = new FirstFragment();
Bundle args = new Bundle();
args.putInt("page", page);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageNum = getArguments().getInt("page", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.workflow_item_layout, container, false);
titleTxt = (TextView) view.findViewById(R.id.title);
topPan = (FrameLayout) view.findViewById(R.id.top_pan);
}
public void setTopPanVisible(boolean isVisible) {
topPan.setVisibility(isVisible);
}
pageChangeListener代码
private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
final int position = pager.getCurrentItem();
if (state == ViewPager.SCROLL_STATE_IDLE) {
final FirstFragment fragment = (FirstFragment) adapter.getItem(position);
fragment.setTopPanVisible(true);
topPan在我调用' setTopPanVisible'时为空在pageChangeListener中。视图如何保持活力以获得价值?
答案 0 :(得分:1)
如果你得到一个空指针异常,那么这意味着两件事之一:
setTopPanVisible()
方法之前,您正在调用onCreateView()
。view.findViewById(R.id.top_pan);
。为了解决问题,你应该弄清楚你遇到了哪些问题。