本文介绍了安卓ViewPager:无法切换页面的onClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,如果有人可以帮我解决这个问题。我有一个按钮设置和点击时,它应该导航到另一个页面viewpager。但每次单击它,应用程序崩溃的fragmenttab2类在返回的onClick错误。这是我的主要活动。

 公共类MainActivity扩展SherlockFragmentActivity {ViewPager viewPager;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    viewPager =(ViewPager)findViewById(R.id.pager);
    viewPager.setAdapter(新ViewPagerAdapter(getSupportFragmentManager()));
    viewPager.setCurrentItem(1);
    }
}

下面是包含onClick的片段:

 公共类FragmentTab1扩展SherlockFragment {ViewPager viewPager;@覆盖
公共无效onActivityCreated(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    活动活动= getActivity();    如果(活动!= NULL){
        addListenerOnButton();
    }}@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){
    查看查看= inflater.inflate(R.layout.fragmenttab1,集装箱,FALSE);
    返回视图。
}公共无效addListenerOnButton(){    按钮nextfrag =(按钮)getView()findViewById(R.id.btn_nextfrag)。
    nextfrag.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            ((ViewPager)viewPager.findViewById(R.id.pager))setCurrentItem(1)。
        }
    });
}

}

日志:

  E / AndroidRuntime显示java.lang.NullPointerException
E / AndroidRuntime com.test.seriessample.FragmentTab1 $ 1.onClick(FragmentTab1.java:58)


解决方案

您viewPager可能是。我也看到viewPager本身就是一个ViewPager并在onclick您搜索的其他的ViewPager的ViewPager这看起来不正确之内。

编辑:

您应该添加这样的事情在你的活动

 公共ViewPager getPager(){
    返回viewPager;
}

而在你的片段的onclick

 ((MainActivity)getActivity())getPager()setCurrentItem(1)。

i wanted to know if someone can help me solve this issue. I have a button setup and when its clicked, its supposed to navigate to another viewpager page. But everytime its clicked, the app crashes returning the error at onClick at fragmenttab2 class. Here is my Main Activity.

public class MainActivity extends SherlockFragmentActivity {

ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
    viewPager.setCurrentItem(1);
    }
}

Here is the fragment containing the onClick:

public class FragmentTab1 extends SherlockFragment {

ViewPager viewPager;

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

    Activity activity = getActivity();

    if (activity != null) {
        addListenerOnButton();
    }

}

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

public void addListenerOnButton() {

    Button nextfrag = (Button) getView().findViewById(R.id.btn_nextfrag);
    nextfrag.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ((ViewPager) viewPager.findViewById(R.id.pager)).setCurrentItem(1);
        }
    });
}

}

The log:

E/AndroidRuntime java.lang.NullPointerException
E/AndroidRuntime com.test.seriessample.FragmentTab1$1.onClick(FragmentTab1.java:58)
解决方案

your viewPager is probably null. I also see that viewPager itself is a ViewPager and in the onClick you search for another ViewPager within that ViewPager which doesn't seem right.

EDIT:

you should add something like this in your activity

public ViewPager getPager() {
    return viewPager;
}

And in the onClick of your Fragment

((MainActivity) getActivity()).getPager().setCurrentItem(1);

这篇关于安卓ViewPager:无法切换页面的onClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:52