[英]How to solve stack memory problems?
我正在嘗試構建一個 android 應用程序,讓用戶在時間限制內進行一些計算。 代碼運行良好,直到我將代碼分成兩部分並創建另一個類來執行其他任務。
我已經將所有相應的包和類文件導入到新類中。代碼中沒有錯誤,但是當我運行應用程序時它崩潰了。我嘗試多次更改代碼但沒有用。 當我將所有代碼組合成一個類時,代碼運行良好。
我得到的錯誤是“java.lang.StackOverflowError:行號上的堆棧大小為 8MB”
**MainActivity.java**
package e.nani.firstattempt;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
public int a1;//random num 1
public int a2;//random num 2;
public TextView textview;
public Button b1;
public Button b2;
public Button b3;
public Button b4;
public int option1;
public int option2;
public int option3;
public int option4;
public int score=0;
TextView scoreid;
int numberofquestions=10;
TextView time;
public int answers[]=new int[4];
Logic c=new Logic();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview=(TextView)findViewById(R.id.sum);
b1=(Button)findViewById(R.id.option1);
b2=(Button)findViewById(R.id.option2);
b3=(Button)findViewById(R.id.option3);
b4=(Button)findViewById(R.id.option4);
time=(TextView)findViewById(R.id.timer);
scoreid=(TextView)findViewById(R.id.scoreid) ;
scoreid.setText((0+"/"+numberofquestions));
c.operatio();
timer.start();
}
public void operation(View V)
{
try{
switch(V.getId()) {
case R.id.option1:
if (b1.getText().equals(Integer.toString(option4))) {
score = score + 1;
c.operatio();
scoreid.setText((score +"/"+ numberofquestions));
} else {
Toast.makeText(this, "wrong answer", Toast.LENGTH_SHORT).show();
Vibrator vibrator=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
c.operatio();
}
break;
case R.id.option2:
if (b2.getText().equals(Integer.toString(option4))) {
score = score + 1;
c.operatio();
scoreid.setText(score+"/"+ numberofquestions);
} else
{
Toast.makeText(this, "wrong answer", Toast.LENGTH_SHORT).show();
Vibrator vibrator=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
c.operatio();
}
break;
case R.id.option3:
if (b3.getText().equals(Integer.toString(option4))) {
score = score + 1;
c.operatio();
scoreid.setText((score+"/"+ numberofquestions));
} else
{
Toast.makeText(this, "wrong answer", Toast.LENGTH_SHORT).show();
Vibrator vibrator=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
c.operatio();
}
break;
case R.id.option4:
if (b4.getText().equals(Integer.toString(option4))) {
score = score + 1;
c.operatio();
scoreid.setText(score+"/"+ numberofquestions);
} else
{
Toast.makeText(this, "wrong answer", Toast.LENGTH_SHORT).show();
Vibrator vibrator=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
c.operatio();
}
break;
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
CountDownTimer timer=new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
time.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
time.setText("done!");
}
};
}
邏輯.java
package e.nani.firstattempt;
import java.util.Random;
class Logic {
MainActivity s=new MainActivity();
public void operatio() {
try {
Random n = new Random();
int n1 = n.nextInt(4);
int n2 = n.nextInt(4);
int n3 = n.nextInt(4);
int n4 = n.nextInt(4);
s.a1 = n.nextInt(51);
s.a2 = n.nextInt(35);
s.option1 = n.nextInt((s.a1 + s.a2) + 1);
s.option2 = n.nextInt((s.a1 + s.a2) + 1);
s.option3 = n.nextInt((s.a1 + s.a2) + 1);
s.option4 = s.a1 + s.a2;
s.answers[n1] = s.option1;
while (n2 == n1) {
n2 = n.nextInt(4);
}
while (s.option2 == s.option1 || s.option2 == s.option4) {
s.option2 = n.nextInt((s.a1 + s.a2) + 1);
}
s.answers[n2] = s.option2;
while (s.option3 == s.option2 || s.option3 == s.option1 || s.option3 == s.option4)
{
s.option3 = n.nextInt((s.a1 + s.a2) + 1);
}
while (n3 == n2 || n3 == n1)
{
n3 = n.nextInt(4);
}
s.answers[n3] = s.option3;
while (n4 == n2 || n4 == n1 || n4 == n3) {
n4 = n.nextInt(4);
}
s.answers[n4] = s.option4;
s.b1.setText(Integer.toString(s.answers[0]));
s.b2.setText(Integer.toString(s.answers[1]));
s.b3.setText(Integer.toString(s.answers[2]));
s.b4.setText(Integer.toString(s.answers[3]));
s.textview.setText(s.a1 + "+" + s.a2);
} catch (Exception e) {
e.printStackTrace();
}
}
}
這里的主要問題是,為什么當代碼僅在主類中時應用程序可以正常工作,而在其他類中編寫某些代碼時卻無法工作? 謝謝你。
在您的 MainActivity 中,您有一個實例化的 Logic 類型的變量 c。 但是在您的 Logic 類中有 MainActivity 類型變量,它嘗試實例化 MainActivity 類。 簡而言之,在 A 類中,您實例化 B 類,它使類 A 等等......順便說一下,您不能直接實例化 AppCompatActivity 類。 所以刪除MainActivity s=new MainActivity();
在您的 Logic.class 中。
希望我能幫到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.