I followed instructions from other questions, but my app crashes. So, one of my activities has around 2,000 lines of code. I want to make another class that just holds the code, and then the particular activity runs the method from the other code holding class, just for organization.

So here is the example I've tried, yet it doesn't work. ActivityA:

public class ActivityA extends Activity {

ActivityB test= new ActivityB();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

  testing();

    }

public void testing() {
      test.codeTest();
}    

Then here is the helper class. ActivityB:

public class ActivityB extends ActivityA{

    public void codeTest() {    
    //2,000 lines of code
    }

The app crashes then. What am I doing wrong? :/ I've also tried:

public class ActivityA extends Activity {

ActivityB test= new ActivityB();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

  test.codeTest();

}

I feel like an idiot for even asking help on this >.<

The error I get is:

java.lang.StackOverflowError.

The lines of errors are:

public class ActivityA extends Activity {

and

public class ActivityB extends ActivityA{

and last line of error code is

ActivityB test= new ActivityB();

What am I doing wrong? :/

有帮助吗?

解决方案

You are asking a valid question. I don't think I have the answer to why your code is crashing. But consider this:

A will contain B, B will have A in it which contains B, which will have A in it which will contain B. . ..

This could be the source of the stack overflow. If you step through the code, at what point does the program crash?

Also, if you may be able to do just fine if you create static class with static functions.

  public static class myUtility {


   public static void someFunction() { . . .}





 }

EDIT: After staring at your code for few minutes, I may have understood what you are trying to do. Let me give it a shot:

Say you want the "dirty" code to be in B. Declare B as abstract:

public abstract class ActivityB extends Activity {
 . . . 

}

Then for your class A,

public class ActivityA extends Activity implements ActivityB {

}

Is that what you are trying to "achieve"?

其他提示

It appears that the issue is related to the fact that you're instantiating Activity B inside of Activity A, but you're intention is to make Activity B an extension of Activity A.

public class ActivityA extends Activity {

ActivityB test= new ActivityB();  // i.e., this line is suspicious Activity B extends A which instantiates B...it's a bit circular... 

Adding to other's answers, you are creating a 2-in-1 activity * infinty. Just declare and initialize your "helper" class as new object in your activity:

MyHelper helper = new MyHelper(context);
// Now, you can access your helper class' functions
String string = helper.helperMethod();

I hope this helps your understanding, happy coding!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top