Android studio app随机耗尽内存

Bax*_*tex 0 java android

我正在构建一个小应用程序,昨天工作得很好,但今天添加按钮后没有.我恢复了所有的更改,但我仍然得到相同的错误,即应用程序内存不足.这是代码和堆栈跟踪:

public class MainActivity extends AppCompatActivity {
private MainActivity mainActivity = new MainActivity();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}}
Run Code Online (Sandbox Code Playgroud)

Logcat:http://pastebin.com/UdAFp32h

我确实在Logcat中找到了一些有趣的东西:

03-29 14:34:42.067 20964-20964/com.example.antongustafsson.csnappen
A/art:art/runtime/runtime.cc:291]   at
com.example.antongustafsson.csnappen.MainActivity.<init>(MainActivity.java:15)

03-29 14:34:42.067 20964-20964/com.example.antongustafsson.csnappen
A/art: art/runtime/runtime.cc:291]   at
com.example.antongustafsson.csnappen.MainActivity.<init>(MainActivity.java:16)

03-29 14:34:42.067 20964-20964/com.example.antongustafsson.csnappen
A/art: art/runtime/runtime.cc:291]   at
com.example.antongustafsson.csnappen.MainActivity.<init>(MainActivity.java:16)

03-29 14:34:42.067 20964-20964/com.example.antongustafsson.csnappen
A/art: art/runtime/runtime.cc:291]   at
com.example.antongustafsson.csnappen.MainActivity.<init>(MainActivity.java:16)

03-29 14:34:42.067 20964-20964/com.example.antongustafsson.csnappen
A/art: art/runtime/runtime.cc:291]   ... **repeated 261584 times**
Run Code Online (Sandbox Code Playgroud)

看起来我有一个无限循环,除了我没有.有谁知道我可以尝试的任何事情?为什么这个错误只是随机发生的?

Som*_*Guy 9

由于变量声明和MainActivity初始化,您的班级会重复创建新的实例MainActivity

MainActivity mainActivity = new MainActivity();
Run Code Online (Sandbox Code Playgroud)

将导致无休止的递归,确实创建实例MainActivity.