-1

I am using dispatchKeyEvent in my FragmentActivity and calling one of my fragments method. Following is code I am using for my FragmentActivity:

public class ViewPagerFragment extends FragmentActivity{
    MyAdapter mAdapter;
    ViewPager mPager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpager_layout);

        mAdapter = new MyAdapter(getSupportFragmentManager());
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        mPager.setCurrentItem(0);
    }

    public void onResume() {
        super.onResume();
    }

    public void onPause() {
        super.onPause();
    }

    public void onDestroy() {
        super.onDestroy();
    }

   @Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        if(event.getKeyCode() == 66){
            FragmentPagerAdapter fragmentPagerAdapter = (FragmentPagerAdapter) mPager.getAdapter();
            ((SecondFragment)fragmentPagerAdapter.getItem(1)).myOnKeyDown(input_str);
        }

        return super.dispatchKeyEvent(event);
    }

    public void onNewIntent(Intent intent) {
        super.onNewIntent(getIntent());
    }

    public static class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
            case 0:
                return new FirstFragment();
            case 1:
                return new SecondFragment();

            default:
                return null;
            }
        }
    }
}

Following is code for my SecondFragment.

public class SecondFragment extends Fragment{
    ProgressBar spinner;

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

    public void onResume(){
        super.onResume();
    }    

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

        spinner = (ProgressBar) view.findViewById(R.id.spinner);

        return view;
    }

    public void myOnKeyDown(String barcode){
        // following line gives NullPointerException
        spinner.setVisibility(ProgressBar.VISIBLE);
    }
}

In my SecondFragment's onCreateView i have initialized ProgressBar which is in SecondFrament layout file. But it always become null when i call myOnKeyDown from dispatchKeyEvent in my FragmentActivity. Why is it so and how can i solve it? Thanks in advance.

Piscean
  • 3,010
  • 12
  • 43
  • 88
  • 1
    I think you need to add more context to help diagnose this problem. It sounds like you have one activity with two fragments, but without more context (and more code) it is difficult to offer any help. – Phil Haigh May 12 '14 at 12:44
  • I have added code. Please have a look. – Piscean May 12 '14 at 13:08

1 Answers1

0

Your SecondFragment's onCreateView() method needs to return the view.

 return view;

Try that.

Garret
  • 1,117
  • 9
  • 17