0

I have 4 tabs in my Activity, and I am changing them with swipe. So I am using 4 Fragments, for the tabs. Swipe is working perfectly. But my issue is that, in my Tab1, I have a list view and every row of list view contain two buttons. So the button click is also working perfectly. But when I am swiping from Tab1 to Tab3, and again movin to Tab1, and after that as I am clicking on the button, it is showing "unable to pause activity java.lang.nullpointerexception". I don't know what is happening. Here is my code of Adapter class where I am implementing the button click. Please help me.

 public class StoreListViewAdapter extends ArrayAdapter<Item> {
Context context;
private ArrayList<Item> items;
private LayoutInflater layoutInflater;

public StoreListViewAdapter(Context context, int resourceId,
        ArrayList<Item> items) {
    super(context, resourceId, items);
    this.context = context;
    this.items = items;
    layoutInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
     EntryItem entryItem = (EntryItem) item;
     final Button buttonView = (Button) view
                .findViewById(R.id.button_view);
        final Button buttonOffline = (Button) view
                .findViewById(R.id.button_offline);
    buttonView.setTag(position);
        buttonView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(context, ViewButtonActivity.class);
                context.startActivity(i);
            }
        });

}

hers is my TabPagerAdapter:-

public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {
    switch (index) {
    case 0:
        return new StoreFragment();
    case 1:
        return new NewsFragment();
    case 2:
        return new EventFragment();
    case 3:
        return new ContactUsFragment();
    }
    return null;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 4;
}

here is my Fragment Activity:-

public class MainActivity extends SherlockFragmentActivity{
private ViewPager viewPager;
private ActionBar actionBar;
Tab tab;

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.ExampleTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    actionBar = getSupportActionBar();
    FragmentManager fm = getSupportFragmentManager();
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    viewPager = (ViewPager) findViewById(R.id.pager);
    ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            // Find the ViewPager Position
            actionBar.setSelectedNavigationItem(position);
        }
    };

    viewPager.setOnPageChangeListener(ViewPagerListener);
    // Locate the adapter class called ViewPagerAdapter.java
    TabsPagerAdapter viewpageradapter = new TabsPagerAdapter(fm);
    // Set the View Pager Adapter into ViewPager
    viewPager.setAdapter(viewpageradapter);

    ActionBar.TabListener tabListener = new ActionBar.TabListener() {

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // Pass the position on tab click to ViewPager
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
    };
    // Create first Tab
    tab = actionBar.newTab().setText("STORE").setTabListener(tabListener);
    actionBar.addTab(tab);

    // Create second Tab
    tab = actionBar.newTab().setText("NEWS").setTabListener(tabListener);
    actionBar.addTab(tab);

    // Create third Tab
    tab = actionBar.newTab().setText("EVENTS").setTabListener(tabListener);
    actionBar.addTab(tab);

    // Create fourth Tab
    tab = actionBar.newTab().setText("CONTACTUS")
            .setTabListener(tabListener);
    actionBar.addTab(tab);
}

here is the Fragment class:-

public class StoreFragment extends SherlockListFragment {
ListView listview;
ArrayList<Item> items = new ArrayList<Item>();
StoreListViewAdapter adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_store, container,
            false);
    listview = (ListView) rootView.findViewById(android.R.id.list);

    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
        }
    });
    return rootView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    items.add(new EntryItem(R.drawable.img1, "Stuff India", "Category:Entertainment",
            "View", "Offline","STUFF is the world's biggest gadget and technology lifestyle magazine with more than 25 editions globally. The Indian edition upholds these international values while showcasing the best gadgets and technology available in India."));
    items.add(new SectionItem("Previous Issue"));
    items.add(new EntryItem(R.drawable.img2, "Medgate Today", "Category:Sports",
            "View", "Offline","Detail Description"));
    items.add(new EntryItem(R.drawable.img3, "HWM Singapor", "Category:News",
            "View", "Offline","Detail Description"));
    items.add(new EntryItem(R.drawable.img4, "Shape Singapore", "Category:LifeStyle",
            "View", "Offline","Detail Description"));
    items.add(new EntryItem(R.drawable.img5, "The Field", "Category:Entertainment",
            "View", "Offline","Detail Description"));
    items.add(new EntryItem(R.drawable.img1, "TV Times", "Category:News",
            "View", "Offline","Detail Description"));

    adapter = new StoreListViewAdapter(getActivity(),
            R.layout.tab_row_list_entry, items);
     listview.setAdapter(adapter);
}

public void onViewButtonClick(View view){
    Intent i = new Intent(getActivity(),ViewButtonActivity.class);
    startActivity(i);
}

here is the logcate error:-

06-23 11:45:11.839: E/AndroidRuntime(3520): FATAL EXCEPTION: main
06-23 11:45:11.839: E/AndroidRuntime(3520): java.lang.RuntimeException: Unable to pause        activity {com.ebizzinfotech.magazineapp/com.ebizzinfotech.magazineapp.MainActivity}:    java.lang.NullPointerException
06-23 11:45:11.839: E/AndroidRuntime(3520):     at     android.app.ActivityThread.performPauseActivity(ActivityThread.java:2358)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at    android.app.ActivityThread.performPauseActivity(ActivityThread.java:2315)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2295)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.app.ActivityThread.access$1700(ActivityThread.java:117)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.os.Looper.loop(Looper.java:130)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.app.ActivityThread.main(ActivityThread.java:3687)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at java.lang.reflect.Method.invokeNative(Native Method)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at java.lang.reflect.Method.invoke(Method.java:507)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at dalvik.system.NativeStart.main(Native Method)
06-23 11:45:11.839: E/AndroidRuntime(3520): Caused by: java.lang.NullPointerException
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at com.actionbarsherlock.app.SherlockFragmentActivity.onSaveInstanceState(SherlockFragmentActivity.java:126)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.app.Activity.performSaveInstanceState(Activity.java:1037)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1181)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2340)
06-23 11:45:11.839: E/AndroidRuntime(3520):     ... 12 more
Amit Jayaswal
  • 1,725
  • 2
  • 19
  • 36

1 Answers1

0

I sloved the problem. @shayan pourvatan helps me. I am posting the solution for helping others. Actually it was giving the error because of ActionBarSherlock compatibility library. The workaround is that your adapter extends FragmentStatePagerAdapter instead of FragmentPagerAdapter.

Amit Jayaswal
  • 1,725
  • 2
  • 19
  • 36