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

时间:2013-07-05 05:08:19

标签: java android stack-overflow

我不知道该怎么做。我看过logcat猫,我知道它在我的MainActivity类和我的DrawingTools类之间。我还列出了行号,以帮助识别哪些行。我的问题是如何解决这个错误java.lang.StackOverflow?

这是我的Logcat:

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  }

1 个答案:

答案 0 :(得分:3)

你有两个类的循环引用

DrawingTools类声明一个callMain = new MainActivity();,它将在init

上创建

另一方面,MainActivity类声明DrawingTools callDT= new DrawingTools();,它也将在init ...

提供

在这种情况下,一个类依赖于另一个构建,反之亦然。