[英]Memory Issues Android App
When I run my app, it instantly crashes. 当我运行我的应用程序时,它立即崩溃。 I used the Android Monitor and it looks like when the app starts it uses more memory than allocated (there is a screenshot below showing this).
我使用了Android Monitor,它看起来像是在应用启动时使用的内存多于分配的内存(下面的屏幕快照显示了此情况)。 I have only developed for computers so I didn't worry about memory, but it seems that even two moderately sized arrays pushed it over the limit.
我只是为计算机开发的,所以我不必担心内存,但是似乎即使两个中等大小的阵列也将其推到了极限。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//input initialization
final EditText numberInput = (EditText) findViewById(R.id.number_input);
Button submit = (Button) findViewById(R.id.submit_button);
Button zero = (Button) findViewById((R.id.resetButton));
//progress bar initialization
final ProgressBar progressBars[] = null;
progressBars[0] = (ProgressBar)findViewById(R.id.progressBar0);
progressBars[1] = (ProgressBar)findViewById(R.id.progressBar1);
progressBars[2] = (ProgressBar)findViewById(R.id.progressBar2);
progressBars[3] = (ProgressBar)findViewById(R.id.progressBar3);
progressBars[4] = (ProgressBar)findViewById(R.id.progressBar4);
progressBars[5] = (ProgressBar)findViewById(R.id.progressBar5);
progressBars[6] = (ProgressBar)findViewById(R.id.progressBar6);
progressBars[7] = (ProgressBar)findViewById(R.id.progressBar7);
progressBars[8] = (ProgressBar)findViewById(R.id.progressBar8);
progressBars[9] = (ProgressBar)findViewById(R.id.progressBar9);
progressBars[10] = (ProgressBar)findViewById(R.id.progressBar10);
progressBars[11] = (ProgressBar)findViewById(R.id.progressBar11);
progressBars[12] = (ProgressBar)findViewById(R.id.progressBar12);
progressBars[13] = (ProgressBar)findViewById(R.id.progressBar13);
progressBars[14] = (ProgressBar)findViewById(R.id.progressBar14);
progressBars[15] = (ProgressBar)findViewById(R.id.progressBar15);
progressBars[16] = (ProgressBar)findViewById(R.id.progressBar16);
progressBars[17] = (ProgressBar)findViewById(R.id.progressBar17);
//variable value initialization
final TextView textViews[] = null;
textViews[0] = (TextView)findViewById(R.id.value0);
textViews[1] = (TextView)findViewById(R.id.value1);
textViews[2] = (TextView)findViewById(R.id.value2);
textViews[3] = (TextView)findViewById(R.id.value3);
textViews[4] = (TextView)findViewById(R.id.value4);
textViews[5] = (TextView)findViewById(R.id.value5);
textViews[6] = (TextView)findViewById(R.id.value6);
textViews[7] = (TextView)findViewById(R.id.value7);
textViews[8] = (TextView)findViewById(R.id.value8);
textViews[9] = (TextView)findViewById(R.id.value9);
textViews[10] = (TextView)findViewById(R.id.value10);
textViews[11] = (TextView)findViewById(R.id.value11);
textViews[12] = (TextView)findViewById(R.id.value12);
textViews[13] = (TextView)findViewById(R.id.value13);
textViews[14] = (TextView)findViewById(R.id.value14);
textViews[15] = (TextView)findViewById(R.id.value15);
textViews[16] = (TextView)findViewById(R.id.value16);
textViews[17] = (TextView)findViewById(R.id.value17);
submit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//get string from input and convert it into an integer
int numberInt = Integer.parseInt(numberInput.getText().toString());
//set the values of the progress bars
for(int i = 0; i < progressBars.length; i++)
{
progressBars[i].setProgress(numberInt);
}
//set the value of the variable display
for (int i = 0; i < textViews.length; i++)
{
textViews[i].setText(numberInt);
}
}
});
zero.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//reset the progress bars to 0
for(int i = 0; i < progressBars.length; i++)
{
progressBars[i].setProgress(0);
}
//reset the variable display to "~~~"
for (int i = 0; i < textViews.length; i++)
{
textViews[i].setText("~~~");
}
}
});
}
I have already tried the Help > Edit Custom VM Options... which instead of fixing the issue caused Android Studio to refuse to open. 我已经尝试过“帮助”>“编辑自定义VM选项” ...,该问题未能解决问题,导致Android Studio拒绝打开。 I am new to Java, so am I missing a memory leak, is there a more efficient way to store the items in the two arrays, or do I just need to increase the memory for my app?
我是Java的新手,所以我是否错过了内存泄漏,是否有更有效的方式将项目存储在两个数组中,还是只需要增加应用程序的内存?
You need to learn a little more about basic Java structures. 您需要了解更多有关基本Java结构的知识。
You are not allocating any memory for your arrays. 您没有为阵列分配任何内存。 Java is not like JavaScript - it won't automatically allocate space for array elements you assign to.
Java与JavaScript不一样-Java不会自动为您分配给其的数组元素分配空间。
final ProgressBar progressBars[] = null;
progressBars[0] = (ProgressBar)findViewById(R.id.progressBar0);
This snippet will fail because the array is null (it has no allocated space for any elements). 此代码段将失败,因为数组为null(没有为任何元素分配的空间)。 When you try to assign an element to it, it will throw a
NullPointerException
. 当您尝试为其分配元素时,它将抛出
NullPointerException
。 You must allocate the space when you create the array: 创建数组时必须分配空间:
final ProgressBar progressBars[] = new ProgressBar[18];
progressBars[0] = (ProgressBar)findViewById(R.id.progressBar0);
My advice would be to run through some Java tutorials first. 我的建议是首先运行一些Java教程。 The comments about viewing
Logcat
should also be heeded - when your App crashes, it will (usually!) tell you exactly where in your source the crash occurred and why. 还应注意有关查看
Logcat
的评论-当您的应用崩溃时,它(通常会!)会告诉您确切的崩溃源以及发生原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.