3

I am very new in Andriod. i have tried so many times but got stuck on this problem since long time. I have an activity named ProductListActivity and in this activity i have one fragments. now i want to display List and for that i have created my own custom array adapter. its working fine when when i am not using fragments but for fragments giving me NullPointerException. I debug my code. Please help me out guys.Thanks for considering. Here i am pasting my code.

Activity class:

public class ProductListActivity extends Activity implements ProductInterface{
public static String cookie;
public static String jsonSettingsResponse;
public static String[] tids;
public static String jsonPorductsCategoryListResponseString;
public JSONArray jsonPorductsCategoryListResponseArray;
public static String vid;
public static String publicPath;
public static JSONArray productsList;
public ArrayList<String> listItems;
public String[] listProCategory;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.product_category_and_list);
    Intent intent = getIntent();
    cookie = intent.getStringExtra(BConstant.WEB_SERVICES_COOKIES);
    productsList = new JSONArray();
    FetchProductCategories products = new FetchProductCategories();
    productsList = products.fetchProducts();
    listProCategory = products.onDisplayProductList(productsList).toArray(
            new String[0]);

    Bundle bundle = new Bundle();
    bundle.putString(BConstant.WEB_SERVICES_COOKIES, cookie);
    bundle.putString(BConstant.PUBLIC_PATH, publicPath);
    bundle.putStringArray(BConstant.TAXONOMY_TID, tids);
    bundle.putStringArray(BConstant.PRODUCT_CATEGORY_NAMES, listProCategory);
    ProductCategoryFragment productCategoryFragment = new ProductCategoryFragment();
    FragmentManager manager = getFragmentManager();
    productCategoryFragment.setArguments(bundle);

}

private class FetchProductCategories {
    protected JSONArray fetchProducts(String... params) {
        jsonSettingsResponse = WebserviceBUtil
                .callWebServicesGetVocabularyList(cookie);
        vid = JSONUtil.parseJSONResponse(jsonSettingsResponse,
                BConstant.TAXONOMY_VID);
        publicPath = JSONUtil.parseJSONResponse(jsonSettingsResponse,
                BConstant.PUBLIC_PATH);
        jsonPorductsCategoryListResponseString = WebserviceBUtil
                .callWebServicesGetProductsList(cookie, vid);
        tids = ProductCategoryIds.parseJSONResponseToGetTidsOfProducts(
                jsonPorductsCategoryListResponseString,
                BConstant.TAXONOMY_TID);
        try {
            jsonPorductsCategoryListResponseArray = new JSONArray(
                    jsonPorductsCategoryListResponseString);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonPorductsCategoryListResponseArray;
    }


    protected ArrayList<String> onDisplayProductList(JSONArray result) {
        listItems = new ArrayList<String>();
        for (int i = 0; i < result.length(); i++) {
            try {
                listItems
                        .add(result.getJSONObject(i)
                                .getString(BConstant.NAME_CONSTANT)
                                .toString());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return listItems;
    }
}


    @Override
    public HashMap<Integer, Bitmap> DownloadImages(HashMap<Integer, String> productCategoryImagePath) {
        HashMap<Integer, Bitmap> imgBitmap = new HashMap<Integer, Bitmap>();
        for (int pos = 0; pos < productCategoryImagePath.size(); pos++) {
            Bitmap bval = ImageDownloader
                    .getBitmapFromURL(productCategoryImagePath.get(pos));
        imgBitmap.put(pos, bval);
        }
        return imgBitmap;

}
}

Fragments class:

public class ProductCategoryFragment extends Fragment {
ProductInterface productInterface;
private static String[] tids;
private static HashMap<Integer, String> productCategoryImagePath;
private static String jsonPorductsDetailsImagePathResponse;
private static String publicPath;
private static String cookie;
public String[] listProCategory;
public  ListView listOfProductsCategory;
private static HashMap<Integer, Bitmap> imgBitmapUrls;
DisplayProductListArrayAdapter adapter;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
     View fragProcatView = inflater.inflate(R.layout.product_category_list, container,
                false);
     listOfProductsCategory = (ListView) getActivity().findViewById(R.id.productCategorylistView);
    return fragProcatView;

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    publicPath = getArguments().getString(BConstant.PUBLIC_PATH);
    cookie = getArguments().getString(BConstant.WEB_SERVICES_COOKIES);
    tids = getArguments().getStringArray(BConstant.TAXONOMY_TID);
    listProCategory = getArguments().getStringArray(
            BConstant.PRODUCT_CATEGORY_NAMES);
    Bundle b = getActivity().getIntent().getExtras();
    publicPath = b.getString(BConstant.PUBLIC_PATH);
    cookie = b.getString(BConstant.WEB_SERVICES_COOKIES);
    tids =b.getStringArray(BConstant.TAXONOMY_TID);
    listProCategory =b.getStringArray(
            BConstant.PRODUCT_CATEGORY_NAMES);
    productInterface = (ProductInterface) getActivity();
    productCategoryImagePath = new HashMap<Integer, String>();
    for (int i = 0; i < tids.length; i++) {
        jsonPorductsDetailsImagePathResponse = WebserviceBUtil
                .callWebServicesGetProductsDetails(cookie, tids[i]);
        String filename = ProductCategoryIds
                .parseJSONResponseToGetVidOfProductsFromVocabulary(
                        jsonPorductsDetailsImagePathResponse,
                        BConstant.FILE_NAME);
        String completeUrl = publicPath + filename;
        productCategoryImagePath.put(i, completeUrl);
    }
    imgBitmapUrls = productInterface
            .DownloadImages(productCategoryImagePath);
     adapter = new DisplayProductListArrayAdapter(
                getActivity(), listProCategory,
                imgBitmapUrls);
    listOfProductsCategory.setAdapter(adapter);
}


/**
 * ExtendsArrayAdapterClassToDisplayCustomListView
 */
public class DisplayProductListArrayAdapter extends ArrayAdapter<String> {

    Context context;
    HashMap<Integer, Bitmap> prodctImgs;
    String[] proCategoryNames;
    HashMap<Integer, Bitmap>biturls;
    DisplayProductListArrayAdapter(Context c,
            String[] listCategory, HashMap<Integer, Bitmap> imgUrls) {
        super(c,
                R.layout.product_category_single_layout,
                R.id.productCategoryName, listCategory);
        this.context = c;
        this.prodctImgs = imgUrls;
        this.proCategoryNames = listCategory;
        this.biturls = imgUrls;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //LayoutInflater inflater = (LayoutInflater) context
    //          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LayoutInflater inflater=((Activity)context).getLayoutInflater(); 
View row = inflater.inflate(R.layout.product_category_single_layout,parent,false);
        ImageView productCategoryImage = (ImageView) row
                .findViewById(R.id.productCategoryImageId);
        Bitmap bitmap = imgBitmapUrls.get(position);
        // productCategoryImage.setFocusable(false);
        TextView productCategoryName = (TextView) row
                .findViewById(R.id.productCategoryName);
        productCategoryImage.setImageBitmap(bitmap);
        productCategoryName.setText(proCategoryNames[position]);
        return row;
    }
     }
  }

1 Answer 1

2

Change

listOfProductsCategory = (ListView) getActivity().findViewById(R.id.productCategorylistView);

to

listOfProductsCategory = (ListView) fragProcatView .findViewById(R.id.productCategorylistView);

findViewById looks for a view with the id in the current infalted layout. So use the view object to initialize your listview.

Edit:

You do something like this

  static ProductCategoryFragment newInstance(int num) {
    ProductCategoryFragment f = new ProductCategoryFragment();

    // Supply num input as an argument.
    Bundle args = new Bundle();
    args.putInt("num", num);
    f.setArguments(args);

    return f;
}

Then

int value = getArguments().getInt("num");

In Activity

 ProductCategoryFragment newFragment = ProductCategoryFragment.newInstance(10);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Raghunandan for yor help, but in my fragment class code i am getting null pointer exception. in below lines : publicPath = getArguments().getString(BsharpConstant.PUBLIC_PATH); cookie = getArguments().getString(BsharpConstant.WEB_SERVICES_COOKIES); tids = getArguments().getStringArray(BsharpConstant.TAXONOMY_TID); listProCategory = getArguments().getStringArray( BsharpConstant.PRODUCT_CATEGORY_NAMES);
@RRR all those variables are null
but i am doing this in bundle.putString(BsharpConstant.WEB_SERVICES_COOKIES, cookie); bundle.putString(BsharpConstant.PUBLIC_PATH, publicPath); bundle.putStringArray(BsharpConstant.TAXONOMY_TID, tids); bundle.putStringArray(BsharpConstant.PRODUCT_CATEGORY_NAMES, listProCategory); productCategoryFragment.setArguments(bundle);
@RRR nope not right. you need to pass values from activity to fragment??
@RRR also this lin might help stackoverflowk.com/questions/12739909/…. Check accepted answer and comment by prosperk

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.