Android中使用OnClickItemListener时出现OutOfMemoryError错误
请帮助我,我正面临这个问题。我正在粘贴代码: 我试过了,但没能找出我遗漏了什么。我使用两个片段显示两个列表。现在,当用户单击第一个列表项时,我想更改第二个片段中的列表数据。默认情况下,将选择零位索引以在第二个列表中显示数据。我正在使用自定义阵列适配器在两个片段中显示列表。请帮助我。谢谢考虑。在这里,我将代码粘贴到出现以下错误的位置: 完整代码: 第二段:Android中使用OnClickItemListener时出现OutOfMemoryError错误,android,android-fragments,Android,Android Fragments,请帮助我,我正面临这个问题。我正在粘贴代码: 我试过了,但没能找出我遗漏了什么。我使用两个片段显示两个列表。现在,当用户单击第一个列表项时,我想更改第二个片段中的列表数据。默认情况下,将选择零位索引以在第二个列表中显示数据。我正在使用自定义阵列适配器在两个片段中显示列表。请帮助我。谢谢考虑。在这里,我将代码粘贴到出现以下错误的位置: 完整代码: 第二段: public class ProductListFragment extends Fragment implements OnItemCli
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;
}
}
这是我的位图代码:
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()方法中的位图实现造成的 试试这个 下面是一个例子
此错误是由于适配器中的getView()方法中的位图实现造成的 试试这个 下面是一个例子
在listview中处理位图可能很棘手。您可能想考虑使用第三方库,可以帮助您管理列表中的位图,以避免内存不足问题。尝试在listview中处理位图可能很棘手。您可能想考虑使用第三方库,可以帮助您管理列表中的位图,以避免内存不足问题。试试看请给我们看logcat(我将在进一步请求pslc时缩短它)请检查我已将其粘贴在您的图像上方您的图像太大了。在StackOverflow上有很多关于如何在加载时缩小它们的答案。检查-请检查android文档以了解此错误:请向我们显示logcat(我将在进一步请求pslc时缩短它)请检查我已将其粘贴在您的图像上方。您的图像太大。在StackOverflow上有很多关于如何在加载它们时缩小它们的答案。检查-请检查android文档以了解此错误:谢谢大家。正如您所看到的,我正在使用上面的代码,McClickListener和list将在第二个片段中得到更改,默认情况下,第一个list将显示出来。如何改进代码。谢谢大家的支持,谢谢大家。正如您所看到的,我正在使用上面的代码,McClickListener和list将在第二个片段中得到更改,默认情况下,第一个list将显示出来。如何改进代码。谢谢你们所有人的支持。我如何使用这个图书馆,正如我在我的帖子中提到的,我对此非常陌生。你能告诉我吗?无论如何,谢谢你的帮助。我建议浏览链接,下载示例应用程序,看看这个库是如何使用的。我如何使用这个库,正如我在帖子中提到的,我对此很陌生。你能告诉我吗?无论如何,谢谢你的帮助。我建议浏览链接,下载示例应用程序,看看库是如何使用的。
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;
}