0

I am trying to validate fragment on swipe.

but it gives me error on logcat- Exception dispatching input event. Exception in MessageQueue callback: handleReceiveCallback java.lang.NullPointerException

Here is my 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) {
        }
    });

and here is my fragment

@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);
}

//Excuse me for text mistakes

I have also googled ([like this] (How to validate EditText in Fragments and prevent Fragment change?) ) but not find much about this.

Community
  • 1
  • 1
Kumar Bankesh
  • 296
  • 4
  • 27

1 Answers1

1

it throws NPE Exception because income_salary is null , you are calling registerUiWidgets(rootView) from your Activity but doesn't know that if your Fragment View created or not . get your attached Fragment inside your Activity this way, then call it's function :

YourFragment fragment = (YourFragment)getSupportFragmentManager().findFragmentById(R.id.frag_id);
if(fragment != null)
    String canSwipe = fragment.canSwipe();
Arash GM
  • 10,316
  • 6
  • 58
  • 76
  • can you guide me how to do this ? i am not able to attach fragment because of id. My activity isn't giving me id of my fragment. @Arash – Kumar Bankesh Feb 25 '15 at 10:47
  • @Banku : finding with id can be implemented when you are defining your fragment inside Xml Layout , in your situation you have to find it by Tag (findFragmentByTag) which you set Tag when you are instantiating your Fragments inside ViewPager. http://stackoverflow.com/questions/19677218/refresh-a-fragment-from-its-parent-activity – Arash GM Feb 25 '15 at 10:55