我无法弄清楚如何解决我的问题。我希望你能帮助我:)。
每次我用10件物品运行我的RecyclerView一切都还可以。 当我启动应用程序时,我可以在移动设备屏幕上看到4个第一项。 但是当我尝试向下滚动我的RecylcerView时,应用程序崩溃时出现以下错误:" java.lang.OutOfMemoryError"我必须说每个项目都有超过5个图像。
FATAL EXCEPTION: main
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: java.lang.OutOfMemoryError
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.content.res.Resources.loadDrawable(Resources.java:2988)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.widget.ImageView.<init>(ImageView.java:131)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:60)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:56)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.support.v7.internal.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:98)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:926)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:980)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at net.premates.SearchListActivity_old$1.onCreateViewHolder(SearchListActivity_old.java:69)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5223)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4449)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4359)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1161)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1018)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1492)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2416)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.View.dispatchTouchEvent(View.java:7817)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2174)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2174)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2174)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2174)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2174)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2174)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2326)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1612)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.app.Activity.dispatchTouchEvent(Activity.java:2494)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2274)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.View.dispatchPointerEvent(View.java:8025)
11-13 15:03:52.570 12519-12519/net.myname E/AndroidRuntime: at android.view.ViewRootImpl$ViewPostImeInputStage.proce
这是我的主要活动主要活动的重要部分:
public static class MyViewHolder extends RecyclerView.ViewHolder {
public final ImageView image1;
public final ImageView image2;
public final ImageView image3;
public final ImageView image4;
public final ImageView image5;
public final TextView text1;
public final TextView text2;
public final TextView text3;
public MyViewHolder(View v) {
super(v);
image1 = (ImageView) v.findViewById(R.id.imageButton9);
image2 = (ImageView) v.findViewById(R.id.imageButton10);
image3 = (ImageView) v.findViewById(R.id.imageButton11);
image4 = (ImageView) v.findViewById(R.id.imageButton12);
image5 = (ImageView) v.findViewById(R.id.imageButton13);
text1 = (TextView) v.findViewById(R.id.text1_);
text2 = (TextView) v.findViewById(R.id.text2);
text3 = (TextView) v.findViewById(R.id.text3);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_list);
RecyclerView rv = (RecyclerView)findViewById(R.id.my_recycler_view);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(new RecyclerView.Adapter() {
@Override
public int getItemCount() {
return Note.getNoten().size();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = getLayoutInflater().inflate(R.layout.search_list_row, viewGroup, false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
Note note = Note.getNoten().get(i);
MyViewHolder mvh = (MyViewHolder) viewHolder;
mvh.image1.setImageResource(note.image1);
mvh.image2.setImageResource(note.image2);
mvh.image3.setImageResource(note.image3);
mvh.image4.setImageResource(note.image4);
mvh.image5.setImageResource(note.image5);
mvh.text1.setText(note.text1);
mvh.text2.setText(""+note.text2);
mvh.text3.setText( ""+note.text3);
}
});
}
这是我的类注意://它使用来自mysql数据库的数据填充我的ArrayList
public class Note {
public final String text1;
public final int int1;
public final int image1;
public final int image2;
public final int image3;
public final int image4;
public final int image5;
public final int int2;
static InputStream is;
static ArrayList<String> results = new ArrayList<String>();
static JSONObject json_data;
static Boolean dataComplete = false;
public Note(String text1, int int1, int int2, int image1, int image2, int image3, int image4, int image5) {
this.text1 = text1;
this.int1 = int1;
this.int2 = int2;
this.image1 = image1;
this.image2 = image2;
this.image3 = image3;
this.image4 = image4;
this.image5 = image5;
}
private static List<Note> noten;
public synchronized static List<Note> getNoten() {
if (noten == null) {
noten = new ArrayList<Note>();
getData();
while(!dataComplete) // es soll solange gewartet werden bis die ArrayList ganz gefüllt ist und die Abfrage beendet wurde
{
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return noten;
}
public static void getData() {
Thread t = new Thread() {
private String result = "";
public void run()
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("**********");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Fehler bei der http Verbindung " + e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
//results.add((String) json_data.get("ID") + " "+ json_data.get("text1"));
Log.e("log_tag", (String) json_data.get("int2") + " " + json_data.get("int1"));
int image1_status = Integer.parseInt((String) json_data.get("image1"));
int image2_status = Integer.parseInt((String) json_data.get("image2"));
int image3_status = Integer.parseInt((String) json_data.get("image3"));
int image4_status = Integer.parseInt((String) json_data.get("image4"));
int image5_status = Integer.parseInt((String) json_data.get("image5"));
if( image1_status == 1)
image1_status = R.drawable.image1_selected;
else
image1_status = R.drawable.image1_unselected;
if( image2_status == 1)
image2_status = R.drawable.image2_selected;
else
image2_status = R.drawable.image2_unselected;
if( image3_status == 1)
image3_status = R.drawable.image3_selected;
else
image3_status = R.drawable.image3_unselected;
if( image4_status == 1)
image4_status = R.drawable.image4_selected;
else
image4_status = R.drawable.image4_unselected;
if( image5_status == 1)
image5_status = R.drawable.image5_selected;
else
image5_status = R.drawable.image5_unselected;
noten.add(new Note((String) json_data.get("text1"), Integer.parseInt((String) json_data.get("int2")), Integer.parseInt((String)json_data.get("int1")), image1_status,image2_status,image3_status,image4_status,image5_status));
}
dataComplete = true;
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
};
t.start();
}
}
这是我的search_list-row:你可以看到我使用了很多图片,但我需要它们:/
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="65dp"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:src="@drawable/player"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginRight="@dimen/margin"
android:layout_marginTop="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/text1_"
android:id="@+id/text1"
android:textColor="@color/colorPrimaryText"
android:layout_marginTop="@dimen/margin"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/imageView2"
android:layout_toEndOf="@+id/imageView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/player_name"
android:id="@+id/text1_"
android:layout_alignTop="@+id/text1"
android:layout_toRightOf="@+id/text1"
android:layout_marginLeft="@dimen/margin"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/text2"
android:id="@+id/text2_"
android:layout_below="@+id/text1"
android:layout_toRightOf="@+id/imageView2"
android:layout_toEndOf="@+id/imageView2"
android:textColor="@color/colorPrimaryText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/rank_"
android:id="@+id/text2"
android:layout_alignTop="@+id/text2_"
android:layout_alignLeft="@+id/text1_"
android:layout_alignStart="@+id/text1_"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<LinearLayout
android:orientation="horizontal"
android:id="@+id/linearLayout4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text3_"
android:layout_alignRight="@+id/text3"
android:layout_alignEnd="@+id/text3"
android:layout_marginRight="@dimen/margin"
android:layout_marginTop="@dimen/margin"
android:layout_marginBottom="@dimen/margin"
android:layout_toRightOf="@+id/imageView2"
android:layout_toEndOf="@+id/imageView2">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton9"
android:src="@drawable/image1_unselected"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_weight="1"
android:background="@android:color/transparent"
android:layout_marginRight="@dimen/margin" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton10"
android:src="@drawable/image2_unselected"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_weight="1"
android:background="@android:color/transparent"
android:layout_marginRight="@dimen/margin" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton11"
android:src="@drawable/image3_unselected"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_weight="1"
android:background="@android:color/transparent"
android:layout_marginRight="@dimen/margin" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton12"
android:src="@drawable/image4_unselected"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_weight="1"
android:background="@android:color/transparent"
android:layout_marginRight="@dimen/margin" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton13"
android:src="@drawable/image5_unselected"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_weight="1"
android:background="@android:color/transparent"
android:layout_marginRight="@dimen/margin" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/text3_"
android:id="@+id/text3_"
android:layout_toRightOf="@+id/imageView2"
android:layout_toEndOf="@+id/imageView2"
android:textColor="@color/colorPrimaryText"
android:layout_below="@+id/text2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/text3"
android:id="@+id/text3"
android:layout_alignTop="@+id/text3_"
android:layout_alignRight="@+id/text2"
android:layout_alignEnd="@+id/text2"
android:layout_alignLeft="@+id/text2"
android:layout_alignStart="@+id/text2" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/linearLayout4"
android:layout_alignRight="@+id/text3"
android:layout_alignEnd="@+id/text3"
android:background="@color/colorDivider"></FrameLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/message"
android:layout_alignBottom="@+id/linearLayout4"
android:src="@drawable/message"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:background="@android:color/transparent"
android:layout_marginLeft="6dp"
android:layout_alignRight="@+id/imageView2"
android:layout_alignEnd="@+id/imageView2" />
</RelativeLayout>
我希望有人可以帮助我:)坦克!
Edite:如果此信息很重要,我会使用Android Studio:)