2013-12-20 96 views
1

請幫助我解決此問題。在這裏,我粘貼我的代碼:Android中使用OnClickItemListener時出現OutOfMemoryError

我已經嘗試過,但無法找出我失蹤的東西。我使用兩個片段兩個顯示列表。現在我想在用戶單擊第一個列表項時更改第二個片段中的列表數據。默認情況下,將選擇零位索引來顯示第二個列表中的數據。我正在使用自定義數組適配器來顯示這兩個片段中的列表。請幫助我們。感謝您的考慮。在這裏,我貼我的代碼,我正在此錯誤:

完整代碼: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; 
     } 
+1

請向我們展示logcat(我會在進一步的請求pslc中縮短它) –

+0

請檢查我已粘貼 – rup35h

+0

您的圖片太大。在StackOverflow上有很多答案,可以幫助您在加載時縮小它們的大小。 – Simon

回答

2

這個錯誤是因爲在getView()在適配器中位圖的實現方法的

試試這個

CreatScaledBitmap

我這裏是

Example 1

+0

感謝大家。正如你可以看到上面的代碼我使用onItemclickListener和列表將獲得第二個片段的變化,默認情況下,第一個列表將顯示。我如何改進我的代碼。感謝大家的支持。 – rup35h

2

處理位圖在ListView可能會很棘手。您可能需要考慮使用可幫助您管理列表中的位圖的第三方庫,以避免內存不足問題。嘗試Universal Image Loader

+0

我如何使用這個庫,正如我在我的文章中提到的,我對此很新。你能告訴我嗎?無論如何感謝您的幫助。 – rup35h

+0

我建議瀏覽鏈接,下載示例應用程序,並查看庫的使用方式。 –

相關問題