I have an arraylist of hashmaps inside the fragment activity class like this. This is inside OnCreate method.
try {
pp = new Parser(getQuery());
productData = pp.getData(asynctask, getQuery());
Log.v("data size:", ""+productData.size()+"");
} catch (URISyntaxException e) {
e.printStackTrace();
}
public ArrayList<HashMap<String, String>> getProductData()
{
return this.productData;
}
Here it works fine and data loaded correctly. And the variable productData is declared as public.
Here is my fragment adapter class:
public class ProductViewPagerAdapter extends FragmentPagerAdapter {
ProductViewActivity productViewActivity = new ProductViewActivity();;
public ProductViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
int temp = productViewActivity.getProductData().size();
Log.v("size:",""+temp+"");
Fragment fragment = new ProductViewFragment();
Bundle args = new Bundle();
args.putInt(ProductViewFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
Here the size gives 0. That ends up in Null pointer exception. Where is the issue here?
That’s your problem:
ProductViewActivity productViewActivity = new ProductViewActivity();
You need to pass the
instanceof the Activity in the constructor ofProductViewPagerAdapterOR set it explicitly with a setter function.
Then change
To:
Hope this helps