android - 在 Android 中使用 OnClickItemListener 时 Android 中的 OutOfMemoryError

标签 android android-fragments

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

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

完整代码:onItemClickListener between two fragments

第二个 fragment :

 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;
    }
}

这是日志:

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/

相关文章:

java - Android:seekbar.setProgress() 给出 Nullpointer 异常

java - 恢复上一个 fragment onBackPressed()?

android - 使用 FragmentStatePagerAdapter 从 ViewPager 中动态删除一个项目

android - 使用按钮使用 fragment 更改布局

带有 Dagger 2 的 Android 普通 Junit

android - 如何在 OpenGL Android 中镜像纹理位图?

java - Android:如何以编程方式启用/禁用 Wifi 或 Internet 连接

java - Gradle构建失败ProcessException

android - 此构建中使用了已弃用的 Gradle 功能,使其与 Gradle 6 不兼容

java - 从 fragment 内部监听 Activity 按钮点击