如何解决堆栈内存问题?

时间:2019-04-13 12:53:45

标签: java android

iam试图构建一个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!");
                        }
                        };


                        }

Logic.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();
                                    }
                                }
                            }

这里的主要问题是,为什么当代码仅在主类中时应用程序可以正常工作,而在其他类中编写某些代码却不能正常工作? 谢谢。

1 个答案:

答案 0 :(得分:0)

在MainActivity中,具有实例化的Logic类型的变量c。 但是在您的Logic类中有MainActivity类型变量,该变量试图实例化MainActivity类。概括地说,您实例化了A类,实例化了B类,它实例化了A类,依此类推... 顺便说一句,您不能直接实例化AppCompatActivity类。 因此,请在您的Logic.class中删除MainActivity s=new MainActivity();

希望我能为您提供帮助。