首页 > 解决方案 > 如何处理这种类型的异常?

问题描述

我创建了一个包含六个按钮的应用程序仪表板。我的项目代码编译完美,没有显示任何错误。当我第一次单击任何按钮时,它也能完美响应,但是当我第二次单击另一个按钮时,我的应用程序完全崩溃并在“运行”选项卡中显示错误。

我已经用谷歌搜索了很多次,但没有什么可以解决我的关键语句 Seeds class(MainActivity) 在这里:

public class Seeds extends AppCompatActivity {
RecyclerView mRecyclerView;
List<SeedsData> mySeedsList;
SeedsData mSeedsData;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_seeds);

    mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView2);


    GridLayoutManager gridLayoutManager;
    gridLayoutManager = new GridLayoutManager(Seeds.this, 1);
    mRecyclerView.setLayoutManager(gridLayoutManager);

    mySeedsList = new ArrayList<>();
    mSeedsData = new SeedsData("Wheat","Wheat is a grass widely cultivated for its seed, a cereal grain which is a worldwide staple food. The many species of wheat together make up the genus Triticum; the most widely grown is common wheat (T. aestivum). Botanically, the wheat kernel is a type of fruit called a caryopsis.","Rs.50",R.drawable.wheat);
    mySeedsList.add(mSeedsData);

    mSeedsData = new SeedsData("Peanut","Peanut seeds are planted one to two inches deep, one every six inches or so apart, in rows about three feet apart. ... Thirty to forty days after emergence the plants bloom, form and enter the soil. The peanut shells and kernels develop and mature during the next 60 to 70 day period.","Rs.6000",R.drawable.peanut);
    mySeedsList.add(mSeedsData);

    mSeedsData = new SeedsData("Almond","The fruit of the almond is a drupe, consisting of an outer hull and a hard shell with the seed, which is not a true nut, inside. Shelling almonds refers to removing the shell to reveal the seed","Rs.700",R.drawable.almond);
    mySeedsList.add(mSeedsData);

    MyAdapter2   myAdapter2 = new MyAdapter2(Seeds.this,mySeedsList);
    mRecyclerView.setAdapter(myAdapter2);
}
}

SeedsData 类代码在这里:

public class SeedsData {
private String itemName;
private String itemDescription;
private String itemPrice;
private int itemImage;
public SeedsData(String itemName, String itemDescription, String  itemPrice, int itemImage) {
    this.itemName = itemName;
    this.itemDescription = itemDescription;
    this.itemPrice = itemPrice;
    this.itemImage = itemImage;
}

public String getItemName() {
    return itemName;
}

public String getItemDescription() {
    return itemDescription;
}

public String getItemPrice() {
    return itemPrice;
}

public int getItemImage() {
    return itemImage;
}
}

MyAdapter3.java 类代码在这里:

public class MyAdapter3 extends   RecyclerView.Adapter<DiseasesViewHolder>{

private Context mContext;
private List<DiseasesData> myDiseasesList;

public MyAdapter3(Context mContext, List<DiseasesData> mySeedsList) {
    this.mContext = mContext;
    this.myDiseasesList = mySeedsList;
}

@Override
public DiseasesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_row_item3,parent,false);
    return new DiseasesViewHolder(mView);
}


@Override
public void onBindViewHolder(@NonNull final DiseasesViewHolder holder, final int position) {

    holder.imageView.setImageResource(myDiseasesList.get(position).getItemImage());
    holder.mTitle.setText(myDiseasesList.get(position).getItemName());
    holder.mDescription.setText(myDiseasesList.get(position).getItemDescription());
    holder.mPrice.setText(myDiseasesList.get(position).getItemPrice());

   /* SeedsViewHolder.mCardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(mContext,SeedsDetailedActivity.class);
            intent.putExtra("Image",mySeedsList.get(holder.getAdapterPosition()).getItemImage());
            intent.putExtra("Description",mySeedsList.get(holder.getAdapterPosition()).getItemDescription());
            mContext.startActivity(intent);
        }
    });*/
}

@Override
public int getItemCount() {
    return myDiseasesList.size();
}
}

class DiseasesViewHolder extends RecyclerView.ViewHolder      {

static ImageView imageView;
static TextView mTitle;
static TextView mDescription;
static TextView mPrice;
static CardView mCardView;

public DiseasesViewHolder(View itemView) {
    super(itemView);

    imageView = itemView.findViewById(R.id.ivImage);
    mTitle = itemView.findViewById(R.id.tvTitle);
    mDescription = itemView.findViewById(R.id.tvDescription);
    mPrice = itemView.findViewById(R.id.tvPrice);
    mCardView = itemView.findViewById(R.id.myCardView);
}
}

当我点击错误语句中的给定链接时,它会将我的光标带到这些语句的开头,所以这是我的代码的特定关键部分(因为当我点击异常时光标在这部分的开头闪烁)

 holder.imageView.setImageResource(myDiseasesList.get(position).getItemImage());

这是我的完整堆栈跟踪列表(来自运行选项卡)

 W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x94cd8b20)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.e_agriculture10, PID: 3694
java.lang.OutOfMemoryError
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at  android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
    at android.content.res.Resources.loadDrawable(Resources.java:2110)
    at android.content.res.Resources.getDrawable(Resources.java:700)
    at androidx.appcompat.widget.ResourcesWrapper.getDrawable(ResourcesWrapper.java:130)
    at androidx.appcompat.widget.TintResources.getDrawable(TintResources.java:46)
    at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:465)
    at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:203)
    at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
    at androidx.appcompat.content.res.AppCompatResources.getDrawable(AppCompatResources.java:102)
    at androidx.appcompat.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:86)
    at androidx.appcompat.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:94)
    at com.example.e_agriculture10.MyAdapter3.onBindViewHolder(MyAdapter3.java:37)
    at com.example.e_agriculture10.MyAdapter3.onBindViewHolder(MyAdapter3.java:16)
    at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
    at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
    at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
    at androidx.recyclerview.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:557)
    at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
    at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
    at androidx.recyclerview.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
    at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
    at android.view.View.layout(View.java:14817)
    at android.view.ViewGroup.layout(ViewGroup.java:4631)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
    at  android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
    at android.view.View.layout(View.java:14817)
    at android.view.ViewGroup.layout(ViewGroup.java:4631)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
    at android.view.View.layout(View.java:14817)
    at android.view.ViewGroup.layout(ViewGroup.java:4631)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
    at android.view.View.layout(View.java:14817)
    at android.view.ViewGroup.layout(ViewGroup.java:4631)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
    at android.view.View.layout(View.java:14817)
    at android.view.ViewGroup.layout(ViewGroup.java:4631)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
    at android.widget.LinearLayout.layoutVertica
I/Process: Sending signal. PID: 3694 SIG: 9
 Application terminated.

标签: javaandroid

解决方案


您需要提供有关getItemImage()方法正在做什么的更多信息。基本上没有什么清楚的

基本上它是说您的应用程序正在尝试使用比系统允许的更多的内存。在处理位图时,应在实现中考虑内存管理。如果仅用于在屏幕上显示图像,请尝试使用 Glide 库在您的 app/build.gradle 添加其依赖项

 dependencies{
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0' }

并更换holder.imageView.setImageResource(myDiseasesList.get(position).getItemImage());

 Glide.with(your_context)
                .asBitmap()
                .load("your/Image/Path") ///feed in path of the image
                .centerCrop()
                .into(holder.imageView);

推荐阅读