请帮我解决这个问题。我有两个与RecyclerView的活动。我的问题,当我打开Activity1并按下然后打开Activity2时,它会加载,但滚动时应用程序很遗憾地关闭。
每个活动都有适配器和持有人。我忘了完成onBackPressed的活动吗?我应该放什么代码以及放在哪里?谢谢!
Activity.java
package com.bne;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
public class Festivals extends AppCompatActivity {
String[] names;
String[] location;
int[] images ={};
String[] date;
String[] about;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.festivals);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.Festivals);
names = getResources().getStringArray(R.array.festival_name);
location = getResources().getStringArray(R.array.festival_location);
date = getResources().getStringArray(R.array.festival_date);
about = getResources().getStringArray(R.array.festival_about);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator(new DefaultItemAnimator());
FestivalsAdapter adapter = new FestivalsAdapter(this, names, location, images, date, about);
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(adapter);
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
}
Adapter.java
package com.bne;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FestivalsAdapter extends RecyclerView.Adapter
<FestivalsHolder> {
Context festivals;
String [] name, location, date, about;
int [] images;
public FestivalsAdapter(Context festivals, String [] name, String [] location, int [] images, String [] date, String [] about) {
this.festivals = festivals;
this.name = name;
this.location = location;
this.images = images;
this.date = date;
this.about = about;
}
@Override
public FestivalsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.festivals_recycler, null);
FestivalsHolder festivalsHolder = new FestivalsHolder(view);
Typeface content = Typeface.createFromAsset(festivals.getAssets(), "fonts/cantarell.ttf");
Typeface header = Typeface.createFromAsset(festivals.getAssets(), "fonts/avenir.ttf");
festivalsHolder.name.setTypeface(header, Typeface.BOLD);
festivalsHolder.location.setTypeface(content);
festivalsHolder.date.setTypeface(content);
festivalsHolder.about.setTypeface(content);
return festivalsHolder;
}
@Override
public void onBindViewHolder(FestivalsHolder holder, int position) {
holder.name.setText(name[position]);
holder.location.setText(location[position]);
holder.images.setImageResource(images[position]);
holder.date.setText(date[position]);
holder.about.setText(about[position]);
holder.setPlacesItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(festivals, FestivalDetails.class);
intent.putExtra("Name", name[position]);
intent.putExtra("Location", location[position]);
intent.putExtra("Images", images[position]);
intent.putExtra("Date", date[position]);
intent.putExtra("About", about[position]);
festivals.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return name.length;
}
}
Holder.java
package com.bne;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class FestivalsHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView images;
TextView name, location, date, about;
private ItemClickListener itemClickListener;
public FestivalsHolder(View itemView) {
super(itemView);
images = (ImageView) itemView.findViewById(R.id.festivals_image);
name = (TextView) itemView.findViewById(R.id.festivals_name);
location = (TextView) itemView.findViewById(R.id.festivals_location);
date = (TextView) itemView.findViewById(R.id.festivals_date);
about = (TextView) itemView.findViewById(R.id.festivals_about);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
this.itemClickListener.onItemClick(view, getLayoutPosition());
}
public void setPlacesItemClickListener (ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
}
logcat的:
07-28 21:31:16.602 30354-30354/com.bne E/AndroidRuntime: FATAL EXCEPTION: main Process: com.bne, PID: 30354 java.lang.OutOfMemoryError: Failed to allocate a 7200012 byte allocation with 2024544 free bytes and 1977KB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080) at android.content.res.Resources.loadDrawableForCookie(Resources.java:2658) at android.content.res.Resources.loadDrawable(Resources.java:2563) at android.content.res.Resources.getDrawable(Resources.java:821) at android.content.Context.getDrawable(Context.java:458) at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:353) at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:201) at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:189) at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100) at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:73) at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:81) at com.bne.TerrainsAdapter.onBindViewHolder(TerrainsAdapter.java:51) at com.bne.TerrainsAdapter.onBindViewHolder(TerrainsAdapter.java:12) at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6400) at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6433) at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5377) at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5640) at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:282) at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:336) at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:349) at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:356) at android.support.v7.widget.GapWorker.run(GapWorker.java:387) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5451) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
您的资源中似乎有大尺寸的图像,当您在适配器中加载时会导致此错误。
仅使用布局所需尺寸的图像,否则会导致此错误。