gpt4 book ai didi

out-of-memory - 使用 RecyclerView 时出现 android OutOfMemoryError

转载 作者:行者123 更新时间:2023-12-04 06:00:21 26 4
gpt4 key购买 nike

我正在使用 recyclerview 创建一个应用程序。当我尝试在移动设备上滚动它时,出现以下错误。

10-27 11:26:50.635 30536-30536/com.example.sabudaniel61.ktdc E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817) at android.content.res.Resources.loadDrawable(Resources.java:2854) at android.content.res.Resources.getDrawable(Resources.java:1521) at android.widget.ImageView.resolveUri(ImageView.java:628) at android.widget.ImageView.setImageResource(ImageView.java:361) at com.example.sabudaniel61.ktdc.RecyclerAdapter.onBindViewHolder(RecyclerAdapter.java:41) at com.example.sabudaniel61.ktdc.RecyclerAdapter.onBindViewHolder(RecyclerAdapter.java:15) at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:4402) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3717) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3609) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1859) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1311) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1274) at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1102) at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:959) at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:3062) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791) at android.view.Choreographer.doCallbacks(Choreographer.java:591) at android.view.Choreographer.doFrame(Choreographer.java:560) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5302) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method)

下面是 recyclerview 的布局:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relative1">
<ImageView
android:layout_width="match_parent"
android:layout_height="480dp"
android:scaleType="fitXY"
android:id="@+id/imageView1"/>

<TextView
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/textView1"

android:layout_centerHorizontal="true"
android:layout_below="@+id/imageView1"
android:textSize="35sp"
android:textColor="#263238"/>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="@+id/textView2"

android:layout_centerHorizontal="true"
android:layout_below="@+id/textView1"
android:textSize="20sp"
android:textColor="#263238"/>
</RelativeLayout>

RecyclerView 类:

package com.example.sabudaniel61.ktdc;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

/**
* Created by Sabudaniel61 on 2015-10-15.
*/
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{
HotelData[] hoteldata;public int count;View v1;MainActivity main;

View view;
RecyclerAdapter(HotelData[] hoteldata,MainActivity main) {

this.main=main;
this.hoteldata = hoteldata;

}

@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerlayout, null);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}

@Override
public long getItemId(int position) {

return super.getItemId(position);
}

@Override
public void onBindViewHolder(RecyclerAdapter.ViewHolder viewHolder, int i) {
viewHolder.imageView.setImageResource(hoteldata[i].getImageUrl());
viewHolder.textView1.setText(hoteldata[i].getHotel());
viewHolder.textView2.setText(hoteldata[i].getPlace());



}

@Override
public int getItemCount() {
return hoteldata.length;

}

public class ViewHolder extends RecyclerView.ViewHolder {
TextView textView1, textView2; RecyclerView recyclerView;
ImageView imageView;

public ViewHolder(final View itemLayoutView) {
super(itemLayoutView);
v1=itemLayoutView;
textView1 = (TextView) itemLayoutView.findViewById(R.id.textView1);
textView2 = (TextView) itemLayoutView.findViewById(R.id.textView2);

imageView = (ImageView) itemLayoutView.findViewById(R.id.imageView1);
count=getAdapterPosition();
itemLayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MascotFragment mascotFragment = new MascotFragment();

FrameLayout layout = (FrameLayout) main.findViewById(R.id.frame);
layout.removeAllViews();
FragmentTransaction transaction = main.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame, mascotFragment).commit();
}
});

}
// public void clicks(){
//
// v1.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// count = getAdapterPosition();
// if (count == 1) {
// Toast.makeText(v1.getContext(),"Clicked Bolgatty",Toast.LENGTH_LONG).show();
//
// }
//
// }});
//
//
//
//}

}
}

编辑代码:

package com.example.sabudaniel61.ktdc;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

/**
* Created by Sabudaniel61 on 2015-10-15.
*/
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{
HotelData[] hoteldata;public int count;View v1;MainActivity main;

View view;
RecyclerAdapter(HotelData[] hoteldata,MainActivity main) {

this.main=main;
this.hoteldata = hoteldata;

}

@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerlayout, null);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}

@Override
public long getItemId(int position) {

return super.getItemId(position);
}

@Override
public void onBindViewHolder(RecyclerAdapter.ViewHolder viewHolder, int i) {
viewHolder.imageView.setImageResource(hoteldata[i].getImageUrl());
viewHolder.textView1.setText(hoteldata[i].getHotel());
viewHolder.textView2.setText(hoteldata[i].getPlace());



}

@Override
public int getItemCount() {
return hoteldata.length;

}

public class ViewHolder extends RecyclerView.ViewHolder {
TextView textView1, textView2; RecyclerView recyclerView;
ImageView imageView;

public ViewHolder(final View itemLayoutView) {
super(itemLayoutView);
v1=itemLayoutView;
textView1 = (TextView) itemLayoutView.findViewById(R.id.textView1);
textView2 = (TextView) itemLayoutView.findViewById(R.id.textView2);

imageView = (ImageView) itemLayoutView.findViewById(R.id.imageView1);
Picasso.with(main).load(R.drawable.aryahomepage).into(imageView);
Picasso.with(main).load(R.drawable.bolgattyhomepage).into(imageView);
Picasso.with(main).load(R.drawable.mascothomepage).into(imageView);
Picasso.with(main).load(R.drawable.waterhomepage).into(imageView);
Picasso.with(main).load(R.drawable.samudrahomepage).into(imageView);
Picasso.with(main).load(R.drawable.goldenhomepage).into(imageView);
Picasso.with(main).load(R.drawable.rainhomepage).into(imageView);
Picasso.with(main).load(R.drawable.periyarhomepage).into(imageView);
Picasso.with(main).load(R.drawable.pepperhomepage).into(imageView);
Picasso.with(main).load(R.drawable.nandanamhomepage).into(imageView);
Picasso.with(main).load(R.drawable.tamarindhomepage).into(imageView);












count=getAdapterPosition();
itemLayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MascotFragment mascotFragment = new MascotFragment();

FrameLayout layout = (FrameLayout) main.findViewById(R.id.frame);
layout.removeAllViews();
FragmentTransaction transaction = main.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame, mascotFragment).commit();
}
});

}
// public void clicks(){
//
// v1.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// count = getAdapterPosition();
// if (count == 1) {
// Toast.makeText(v1.getContext(),"Clicked Bolgatty",Toast.LENGTH_LONG).show();
//
// }
//
// }});
//
//
//
//}

}
}

最佳答案

尝试使用 Picasso 从资源可绘制文件夹加载图像

这可以通过使用以下代码来完成:

Picasso.with(context).load(R.drawable.drawableName).into(imageView);

关于out-of-memory - 使用 RecyclerView 时出现 android OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33361044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com