Java Android应用程序内存问题

Java Android应用程序内存问题,java,android,memory,Java,Android,Memory,当我运行我的应用程序时,它会立即崩溃。我使用了安卓显示器,看起来当应用程序启动时,它使用的内存比分配的内存多(下面有一个屏幕截图显示这一点)。我只为计算机开发,所以我不担心内存问题,但似乎即使是两个中等大小的阵列也超出了限制 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(

当我运行我的应用程序时,它会立即崩溃。我使用了安卓显示器,看起来当应用程序启动时,它使用的内存比分配的内存多(下面有一个屏幕截图显示这一点)。我只为计算机开发,所以我不担心内存问题,但似乎即使是两个中等大小的阵列也超出了限制

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("~~~");
            }
        }
    });
}
public类MainActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//输入初始化
最终EditText数字输入=(EditText)findViewById(R.id.number\u输入);
按钮提交=(按钮)findViewById(R.id.submit_按钮);
按钮零=(按钮)findViewById((R.id.resetButton));
//进度条初始化
最终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);
//变量值初始化
最终文本视图文本视图[]=null;
TextView[0]=(TextView)findViewById(R.id.value0);
TextView[1]=(TextView)findViewById(R.id.value1);
TextView[2]=(TextView)findViewById(R.id.value2);
TextView[3]=(TextView)findViewById(R.id.value3);
TextView[4]=(TextView)findViewById(R.id.value4);
TextView[5]=(TextView)findViewById(R.id.value5);
TextView[6]=(TextView)findViewById(R.id.value6);
TextView[7]=(TextView)findViewById(R.id.value7);
TextView[8]=(TextView)findViewById(R.id.value8);
TextView[9]=(TextView)findViewById(R.id.value9);
TextView[10]=(TextView)findViewById(R.id.value10);
TextView[11]=(TextView)findViewById(R.id.value11);
TextView[12]=(TextView)findViewById(R.id.value12);
TextView[13]=(TextView)findViewById(R.id.value13);
TextView[14]=(TextView)findViewById(R.id.value14);
TextView[15]=(TextView)findViewById(R.id.value15);
TextView[16]=(TextView)findViewById(R.id.value16);
TextView[17]=(TextView)findViewById(R.id.value17);
submit.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
//从输入中获取字符串并将其转换为整数
int numberprint=Integer.parseInt(numberInput.getText().toString());
//设置进度条的值
for(int i=0;i
我已经尝试过帮助>编辑自定义虚拟机选项…这并没有解决导致Android Studio拒绝打开的问题。我是Java新手,所以我是否缺少内存泄漏,是否有更有效的方法将项目存储在两个阵列中,或者我只需要增加我的应用程序的内存


您需要进一步了解基本Java结构

您没有为数组分配任何内存。Java不像JavaScript,它不会自动为您分配的数组元素分配空间

final ProgressBar progressBars[] = null;
progressBars[0] = (ProgressBar)findViewById(R.id.progressBar0);
此代码段将失败,因为数组为null(没有为任何元素分配空间)。当您尝试为其分配元素时,它将抛出
NullPointerException
。创建数组时必须分配空间:

final ProgressBar progressBars[] = new ProgressBar[18];
progressBars[0] = (ProgressBar)findViewById(R.id.progressBar0);

我的建议是先浏览一些Java教程。还应该注意有关查看
Logcat
的评论-当你的应用程序崩溃时,它(通常!)会告诉你崩溃发生在源代码中的确切位置以及原因。

你需要更多地了解基本Java结构

您没有为数组分配任何内存。Java不像JavaScript,它不会自动为您分配的数组元素分配空间

final ProgressBar progressBars[] = null;
progressBars[0] = (ProgressBar)findViewById(R.id.progressBar0);
此代码段将失败,因为数组为null(没有为任何元素分配空间)。当您尝试为其分配元素时,它将抛出
NullPointerException
。创建数组时必须分配空间:

final ProgressBar progressBars[] = new ProgressBar[18];
progressBars[0] = (ProgressBar)findViewById(R.id.progressBar0);
我的建议是先浏览一些Java教程