請幫助我解決此問題。在這裏,我粘貼我的代碼: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;
}
請向我們展示logcat(我會在進一步的請求pslc中縮短它) –
請檢查我已粘貼 – rup35h
您的圖片太大。在StackOverflow上有很多答案,可以幫助您在加載時縮小它們的大小。 – Simon