如何修复Java.lang.StackOverFlow异常?

2022-04-12 00:00:00 stack-overflow android java

我不知道在这里该做些什么。我看过Logcat,我知道它位于我的MainActivity类和DrawingTools类之间。我还列出了行号,以帮助识别哪些行是哪些行。我的问题是如何修复此错误java.lang.StackOverflow?

这是我的日志:

07-05 00:55:17.852: E/AndroidRuntime(15504): FATAL EXCEPTION: main
07-05 00:55:17.852: E/AndroidRuntime(15504): java.lang.StackOverflowError
07-05 00:55:17.852: E/AndroidRuntime(15504):    at java.lang.ref.FinalizerReference.add(FinalizerReference.java:48)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at android.os.StrictMode$InstanceTracker.<init>(StrictMode.java:2155)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at android.os.StrictMode.trackActivity(StrictMode.java:1857)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at android.app.Activity.<init>(Activity.java:768)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:13)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.DrawingTools.<init>(DrawingTools.java:9)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testcad.MainActivity.<init>(MainActivity.java:22)
07-05 00:55:17.852: E/AndroidRuntime(15504):    at com.example.testc

这是我的MainActivity类:

13    public class MainActivity extends Activity implements OnClickListener {
14  
15   Handler uiHandler;
16   EditText cl;
17   TextView info;
18   Button enter;
19   Button line;
20   Button arc;
21   DrawingUtils callDU = new DrawingUtils();
22   DrawingTools callDT= new DrawingTools();
23   EditTools callET = new EditTools();
24   Conversion callConversion = new Conversion();
25   
26  @Override
27  protected void onCreate(Bundle savedInstanceState) {
28      super.onCreate(savedInstanceState);
29      setContentView(R.layout.canvas);
30                                                                                                               31 info = (TextView) findViewById(R.id.info);                                                                                             32        enter = (Button)findViewById(R.id.enter);
33       line = (Button) findViewById(R.id.line);
34        arc = (Button) findViewById(R.id.arc);
35        cl = (EditText)findViewById(R.id.cl);
36        
37        uiHandler = new Handler() { public void handleMessage(Message msg) {
38       switch (msg.what) {
39        
40        } Bundle bundle = msg.getData(); String string1 =
41        bundle.getString("P1Key"); String string2 =
42        bundle.getString("P2Key"); info.setText(string1);
43        info.setText(string2); } };
44       
45  }
46
47  @Override
48  public void onClick(View v) {
49      switch (v.getId()) {
50      case R.id.enter:
51          String input = cl.getText().toString();
52          synchronized (callDU.string) {
53              String[] In = input.split(",");
54              callDU.setX(In[0]);
55              callDU.setY(In[1]);
56              callDU.setZ(In[2]);
57              callDU.string.notifyAll();
58          }
59
60          break;
61      case R.id.line:
62
63          break;
64      case R.id.arc:
65
66          break;
67      }
68  };
69     }

这是我的DrawingTools类:

 7    public class DrawingTools {
 8  DrawingUtils callDU = new DrawingUtils();
 9  MainActivity callMain = new MainActivity();
10  public class Line extends Thread {
11      Thread line = new Thread();
12      @Override
13      public void run() {
14          // TODO Auto-generated method stub
15          super.run();
16          //Set Text To (Enter Point 1)
17          Message msg = Message.obtain();
18          Bundle bundle = new Bundle();
19          String setTextP1 = "Enter Point 1.";
20          bundle.putString("P1Key", setTextP1);
21          msg.setData(bundle);
22          //callMain.uiHandler.sendMessage(msg);
23          synchronized(callDU.string){
24              try {
25                  callDU.string.wait();
26              } catch (InterruptedException e) {
27                  // TODO Auto-generated catch block
28                  e.printStackTrace();
29              }
30              
31          String X1 = callDU.getX();
32          String Y1 = callDU.getY();
33          String Z1 = callDU.getZ();
34          callDU.point.add(X1);
35          callDU.point.add(Y1);
36          callDU.point.add(Z1);
37          
38          }
39          Message msg1 = Message.obtain();
40          Bundle bundle1 = new Bundle();
41          String setTextP2 = "Enter Point 2.";
42          bundle1.putString("P2Key", setTextP2);
43          msg1.setData(bundle1);
44          //callMain.uiHandler.sendMessage(msg1);
45          synchronized(callDU.string){
46              try {
47                  callDU.string.wait();
48              } catch (InterruptedException e) {
49                  // TODO Auto-generated catch block
50                  e.printStackTrace();
51              }   
52          String X2 = callDU.getX();
53          String Y2 = callDU.getY();
54          String Z2 = callDU.getZ();
55          callDU.point.add(X2);
56          callDU.point.add(Y2);
57          callDU.point.add(Z2);
58          }
59      }   
60  }

解决方案

您对两个类都有循环引用

DrawingTools类声明将在init上创建的callMain = new MainActivity();

另一方面,MainActivity类声明了DrawingTools callDT= new DrawingTools();,这也将在初始化时提供...

在此方案中,一个类依赖于要构造的另一个类,反之亦然。

相关文章