如何验证片段onSwipe?

时间:2015-02-25 07:52:53

标签: android tabs android-edittext fragment swipe

我正在尝试在滑动时验证片段。

但它在logcat上给我错误 -     异常调度输入事件。     MessageQueue回调中的异常:handleReceiveCallback     显示java.lang.NullPointerException

这是我的activityclass

screen = this;


    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
    getActionBar().setDisplayHomeAsUpEnabled(true); 
    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);  

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            /*actionBar.setSelectedNavigationItem(position);*/
            inc_fragment = new Incomefragment();
             boolean isValid; 
            canSwipe = inc_fragment.canSwipe();
            if(canSwipe.matches("false")){
                isValid = false;
            }
            else
                 isValid = true; 
             int currentPosition = 0;
            // <-- here, you need to check yourself valid or not
                if (!isValid) {
                    viewPager.setCurrentItem(currentPosition);
                }else{
                    viewPager.setCurrentItem(position);
                    currentPosition = position;
                }

        }

        @Override
        public void onPageScrolled(int pos, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int pos) {
        }
    });

这是我的片段

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    context = getActivity().getApplicationContext(); 
    rootView = inflater.inflate(R.layout.tab1, container, false);

 income_salary = (EditText) rootView.findViewById(R.id.salary_income_t);
    return rootView;
}



public String canSwipe()
{
    registerUiWidgets(rootView);
     String swipable = "";

     if(inc_sal.matches(""))
     {
         swipable = "false";
     }
     else
         swipable = "true";


     return swipable;
}
public void registerUiWidgets(View v)
{

    //this line is throwing error what should i do?
    inc_sal = income_salary.getText().toString();

     System.out.println("Income from salary is:"+inc_sal);
}

//请原谅我的文字错误

我也用谷歌搜索过([像这样](How to validate EditText in Fragments and prevent Fragment change?))但对此没有太多了解。

1 个答案:

答案 0 :(得分:1)

它会抛出NPE异常,因为income_salary为空,您从Activity调用 registerUiWidgets(rootView),但不知道您的片段视图是否已创建。 以这种方式将你的附加片段放入你的Activity中,然后调用它的函数:

YourFragment fragment = (YourFragment)getSupportFragmentManager().findFragmentById(R.id.frag_id);
if(fragment != null)
    String canSwipe = fragment.canSwipe();