请帮助我,我正面临这个问题。在这里我粘贴我的代码:

我已经尝试过但无法找出我所缺少的东西。我正在使用两个片段,两个显示列表。现在,我想在用户单击第一个列表项时更改第二个片段中的列表数据。默认情况下,将选择零位置索引以在第二个列表中显示数据。我正在使用自定义阵列适配器在两个片段中显示列表。请帮帮我。谢谢您的考虑。在这里,我将代码粘贴到出现此错误的位置:

完整代码:onItemClickListener between two fragments

第二片段:

 public class ProductListFragment extends Fragment implements OnItemClickListener{
    private static HashMap<Integer, Bitmap> imgBitmapUrls;
    public  ListView listOfProducts;
    ProductInterface productInterfaceForProductList;
    private static String jsonPorductsCategoryListResponse;
    private static String cookie;
    private static String[] productImgPath;
    private static String[] nids;
    private static String[] title;
    private static String[] tids;
    private static String tid;
    private static HashMap<Integer, String> productListImagePath;
    DisplayProductListArrayAdapter productListAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
         View fragProListView = inflater.inflate(R.layout.product_category_list, container,
                    false);
         listOfProducts =(ListView) fragProListView .findViewById(R.id.productCategorylistView);
        return fragProListView;
        }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        productInterfaceForProductList = (ProductInterface) getActivity();



        GetDataFromJsonResponse dataResponse = new GetDataFromJsonResponse();
        dataResponse.dataForCustomArrayAdapter();
        productListAdapter = new DisplayProductListArrayAdapter(
                    getActivity(), title,
                    imgBitmapUrls);
         listOfProducts.setAdapter(productListAdapter);
         listOfProducts.setOnItemClickListener(this);
    }
     static ProductListFragment newInstance(Bundle bundle) {
         ProductListFragment productListFragment = new ProductListFragment();
         productListFragment.setArguments(bundle);
            cookie = bundle.getString(BsharpConstant.WEB_SERVICES_COOKIES);
            tids = bundle.getStringArray(BsharpConstant.TAXONOMY_TID);
            return productListFragment;
        }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

    }


    public class GetDataFromJsonResponse{
        public void dataForCustomArrayAdapter(){
            if(getTid()==null){
                tid = tids[0];
                }
        jsonPorductsCategoryListResponse = WebserviceBsharpUtil.
                    callWebServicesGetProductsList(cookie, getTid());
        nids = ProductCategoryIds.parseJSONResponseToGetTidsOfProducts(
                jsonPorductsCategoryListResponse,
                BsharpConstant.PRODUCT_NODE_ID);
        title = ProductCategoryIds.parseJSONResponseToGetTidsOfProducts(
                jsonPorductsCategoryListResponse,
                BsharpConstant.PRODUCT_TITLE);
        productImgPath = ProductCategoryIds.parseJSONResponseToGetTidsOfProducts(
                jsonPorductsCategoryListResponse,
                BsharpConstant.PRODUCT_IMAGE);
                productListImagePath = new HashMap<Integer, String>();
                for(int i =0;i<productImgPath.length;i++){
                    productListImagePath.put(i, productImgPath[i]);
                }
                System.out.println(productListImagePath);
        imgBitmapUrls = productInterfaceForProductList
                .DownloadImages(productListImagePath);
        }
    }


    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.products_list_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=((Activity)context).getLayoutInflater();
            View row = inflater.inflate(R.layout.products_list_single_layout, parent, false);
            ImageView productCategoryImage = (ImageView) row
                    .findViewById(R.id.productImageId);
            Bitmap bitmap = imgBitmapUrls.get(position);
            // productCategoryImage.setFocusable(false);
            TextView productCategoryName = (TextView) row
                    .findViewById(R.id.productName);
            productCategoryImage.setImageBitmap(bitmap);
            productCategoryName.setText(proCategoryNames[position]);
            return row;
        }
    }

    public void clickedProductCategoryIdByProductCategoryFragment(String tid){
        ProductListFragment.setTid(tid);
    }

    public static String getTid() {
        return tid;
    }

    public static void setTid(String tid) {
        ProductListFragment.tid = tid;
    }
}


这是logcat:

12-20 11:24:38.847: E/AndroidRuntime(937): FATAL EXCEPTION: main
12-20 11:24:38.847: E/AndroidRuntime(937): java.lang.OutOfMemoryError
12-20 11:24:38.847: E/AndroidRuntime(937):  at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
12-20 11:24:38.847: E/AndroidRuntime(937):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:527)
12-20 11:24:38.847: E/AndroidRuntime(937):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:599)
12-20 11:24:38.847: E/AndroidRuntime(937):  at com.lenovo.bsharp.imagedownload.ImageDownloader.getBitmapFromURL(ImageDownloader.java:60)
12-20 11:24:38.847: E/AndroidRuntime(937):  at com.lenovo.bsharp.ProductListActivity.DownloadImages(ProductListActivity.java:119)
12-20 11:24:38.847: E/AndroidRuntime(937):  at com.lenovo.bsharp.ProductListFragment$GetDataFromJsonResponse.dataForCustomArrayAdapter(ProductListFragment.java:100)
12-20 11:24:38.847: E/AndroidRuntime(937):  at com.lenovo.bsharp.ProductListFragment.clickedProductCategoryIdByProductCategoryFragment(ProductListFragment.java:146)
12-20 11:24:38.847: E/AndroidRuntime(937):  at com.lenovo.bsharp.ProductListActivity.clickedProductCategory(ProductListActivity.java:131)
12-20 11:24:38.847: E/AndroidRuntime(937):  at com.lenovo.bsharp.ProductCategoryFragment.onItemClick(ProductCategoryFragment.java:93)
12-20 11:24:38.847: E/AndroidRuntime(937):  at android.widget.AdapterView.performItemClick(AdapterView.java:298)
12-20 11:24:38.847: E/AndroidRuntime(937):  at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
12-20 11:24:38.847: E/AndroidRuntime(937):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2859)
12-20 11:24:38.847: E/AndroidRuntime(937):  at android.widget.AbsListView$1.run(AbsListView.java:3533)
12-20 11:24:38.847: E/AndroidRuntime(937):  at android.os.Handler.handleCallback(Handler.java:615)
12-20 11:24:38.847: E/AndroidRuntime(937):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-20 11:24:38.847: E/AndroidRuntime(937):  at android.os.Looper.loop(Looper.java:137)
12-20 11:24:38.847: E/AndroidRuntime(937):  at android.app.ActivityThread.main(ActivityThread.java:4745)
12-20 11:24:38.847: E/AndroidRuntime(937):  at java.lang.reflect.Method.invokeNative(Native Method)
12-20 11:24:38.847: E/AndroidRuntime(937):  at java.lang.reflect.Method.invoke(Method.java:511)
12-20 11:24:38.847: E/AndroidRuntime(937):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-20 11:24:38.847: E/AndroidRuntime(937):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-20 11:24:38.847: E/AndroidRuntime(937):  at dalvik.system.NativeStart.main(Native Method)
12-20 11:29:38.997: I/Process(937): Sending signal. PID: 937 SIG: 9
12-20 11:29:39.917: E/Trace(952): error opening trace file: No such file or directory (2)


这是我的位图代码:

 try {
            System.out.printf("src", src);
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            System.out.printf("Bitmap", "returned");
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.printf("Exception", e.getMessage());
            return null;
        }

最佳答案

此错误是由于适配器中getView()方法中的位图实现所致

尝试这个

CreatScaledBitmap

这是例子

Example 1

关于android - 在Android中使用OnClickItemListener时在Android中出现OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20698942/

10-09 09:29