gpt4 book ai didi

java - 我想在 Android Studio 中制作一个总统评论应用程序,其中包含每位总统的照片,但我不断收到 OutOfMemoryError

转载 作者:行者123 更新时间:2023-12-01 11:35:53 25 4
gpt4 key购买 nike

我刚刚开始使用Android Studio,对java也有点陌生,所以请原谅低效的代码。

无论如何,当我单击应用程序上主要 Activity 上的按钮将用户带到我想要显示图像的 Activity 时,我的应用程序停止工作并抛出 OutOfMemoryError。

这是MainActivty.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button presidentQuizButton = (Button) findViewById(R.id.presidentQuizButton);
Button reviewPresidentsButton = (Button) findViewById(R.id.reviewPresidentsButton);

presidentQuizButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
//when button is clicked, do something
}
});
reviewPresidentsButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
//when button is clicked, do something
startActivity(new Intent(MainActivity.this, presidentReviewActivity.class));
}
});

}

这里是 PresidentReviewActivity.java:

ImageView imageView;
public int presidentNumber = 0;

private Drawable drawable;
private Drawable [] drawables = null;

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

Button menuButton = (Button) findViewById(R.id.menuButton);
Button nextButton = (Button) findViewById(R.id.nextButton);
Button backButton = (Button) findViewById(R.id.backButton);

drawables = new Drawable[]{
getResources().getDrawable(R.drawable.president1),
getResources().getDrawable(R.drawable.president2),
getResources().getDrawable(R.drawable.president3),
getResources().getDrawable(R.drawable.president4),
getResources().getDrawable(R.drawable.president5),
getResources().getDrawable(R.drawable.president6),
getResources().getDrawable(R.drawable.president7),
getResources().getDrawable(R.drawable.president8),
getResources().getDrawable(R.drawable.president9),
getResources().getDrawable(R.drawable.president10),
getResources().getDrawable(R.drawable.president11),
getResources().getDrawable(R.drawable.president12),
getResources().getDrawable(R.drawable.president13),
getResources().getDrawable(R.drawable.president14),
getResources().getDrawable(R.drawable.president15),
getResources().getDrawable(R.drawable.president16),
getResources().getDrawable(R.drawable.president17),
getResources().getDrawable(R.drawable.president18),
getResources().getDrawable(R.drawable.president19),
getResources().getDrawable(R.drawable.president20),
getResources().getDrawable(R.drawable.president21),
getResources().getDrawable(R.drawable.president22),
getResources().getDrawable(R.drawable.president23),
getResources().getDrawable(R.drawable.president24),
getResources().getDrawable(R.drawable.president25),
getResources().getDrawable(R.drawable.president26),
getResources().getDrawable(R.drawable.president27),
getResources().getDrawable(R.drawable.president28),
getResources().getDrawable(R.drawable.president29),
getResources().getDrawable(R.drawable.president30),
getResources().getDrawable(R.drawable.president31),
getResources().getDrawable(R.drawable.president32),
getResources().getDrawable(R.drawable.president33),
getResources().getDrawable(R.drawable.president34),
getResources().getDrawable(R.drawable.president35),
getResources().getDrawable(R.drawable.president36),
getResources().getDrawable(R.drawable.president37),
getResources().getDrawable(R.drawable.president38),
getResources().getDrawable(R.drawable.president39),
getResources().getDrawable(R.drawable.president40),
getResources().getDrawable(R.drawable.president41),
getResources().getDrawable(R.drawable.president42),
getResources().getDrawable(R.drawable.president43),
getResources().getDrawable(R.drawable.president44),
};

menuButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
finish();
}
});

nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
presidentNumber++;
drawable = drawables[presidentNumber];
imageView.setImageDrawable(drawable);
}
});

这是大部分错误日志:

/image/Wcphi.jpg

最佳答案

而不是将所有可绘制内容存储在数组中。实际上,您可以将资源 id 存储在数组中。

  1. private Drawable [] drawables = null; 替换为 private int[] drawableids

  2. drawables = new Drawable[]{...} 替换为 drawableids = new int[]{...}

    <
  3. 替换

    drawable = drawables[presidentNumber];
    imageView.setImageDrawable(drawable);

drawable = getResource().getDrawable(drawableids[presidentNumber]);
imageView.setImageDrawable(drawable);

尝试按照上述步骤进行更改,然后重试。nullpointerException可能是由于您没有初始化drawableids数组或者您仍在使用drawables数组而不是drawableids引起的。

关于java - 我想在 Android Studio 中制作一个总统评论应用程序,其中包含每位总统的照片,但我不断收到 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29998202/

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