从另一个类引用MainActivity中的RadioButton时出错

时间:2012-08-25 12:29:00

标签: java android radio-button stack-overflow cross-reference

我创建了一个以二进制文件打印出时间的应用程序。 逻辑运行良好,但当我尝试分离逻辑和GUI时遇到了一些问题。似乎存在交叉引用问题,但我似乎无法找到它。

MainActivity:

package se.macke.binarycounter;

import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends Activity 
{
    public TextView text0, text;
    public Timer timer;
    public RadioButton rB00,rB01,rB02,rB03, rB04, rB05, rB06, rB07, rB08, rB09, 
                    rB0A, rB0B, rB0C, rB0D, rB0E, rB0F, rB11, rB12;
RadioButton[] rButtons = new RadioButton[18];

public Executive exec = new Executive(); // This is row 25 in the logcat reference
public GregorianCalendar gregCal = (GregorianCalendar) GregorianCalendar.getInstance();

逻辑类:

/**
 * 
 */
 package se.macke.binarycounter;

import static java.lang.System.out;

import java.util.GregorianCalendar;

import android.widget.RadioButton;

/**
 * @author macke
  *
 */
public class Executive
{
//Converts the numbers to a long string with new rows
RadioButton[] rButtons = new RadioButton[18];
GregorianCalendar gregCal = new GregorianCalendar();
MainActivity m = new MainActivity(); // This is row 21 in the LogCat reference

private int sec = gregCal.get(GregorianCalendar.SECOND);                        // time units
private int min = gregCal.get(GregorianCalendar.MINUTE);
private int hour = gregCal.get(GregorianCalendar.HOUR);

logcat的摘录:

08-25 13:43:53.459: E/AndroidRuntime(383): FATAL EXCEPTION: main
08-25 13:43:53.459: E/AndroidRuntime(383): java.lang.StackOverflowError
08-25 13:43:53.459: E/AndroidRuntime(383):  at java.util.Arrays.binarySearch(Arrays.java:405)
08-25 13:43:53.459: E/AndroidRuntime(383):  at org.apache.harmony.luni.internal.util.ZoneInfo.getOffset(ZoneInfo.java:168)
08-25 13:43:53.459: E/AndroidRuntime(383):  at org.apache.harmony.luni.internal.util.ZoneInfo.getOffset(ZoneInfo.java:162)
08-25 13:43:53.459: E/AndroidRuntime(383):  at java.util.GregorianCalendar.fullFieldsCalc(GregorianCalendar.java:495)
08-25 13:43:53.459: E/AndroidRuntime(383):  at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:658)
08-25 13:43:53.459: E/AndroidRuntime(383):  at java.util.Calendar.complete(Calendar.java:839)
08-25 13:43:53.459: E/AndroidRuntime(383):  at java.util.Calendar.setTimeInMillis(Calendar.java:1339)
08-25 13:43:53.459: E/AndroidRuntime(383):  at java.util.GregorianCalendar.<init>(GregorianCalendar.java:339)
08-25 13:43:53.459: E/AndroidRuntime(383):  at java.util.GregorianCalendar.<init>(GregorianCalendar.java:239)
08-25 13:43:53.459: E/AndroidRuntime(383):  at se.macke.binarycounter.Executive.<init>(Executive.java:20)
08-25 13:43:53.459: E/AndroidRuntime(383):  at se.macke.binarycounter.MainActivity.<init>(MainActivity.java:25)
08-25 13:43:53.459: E/AndroidRuntime(383):  at se.macke.binarycounter.Executive.<init>(Executive.java:21)
08-25 13:43:53.459: E/AndroidRuntime(383):  at se.macke.binarycounter.MainActivity.<init>(MainActivity.java:25)
08-25 13:43:53.459: E/AndroidRuntime(383):  at se.macke.binarycounter.Executive.<init>(Executive.java:21)
08-25 13:43:53.459: E/AndroidRuntime(383):  at se.macke.binarycounter.MainActivity.<init>(MainActivity.java:25)
08-25 13:43:53.459: E/AndroidRuntime(383):  at se.macke.binarycounter.Executive.<init>(Executive.java:21)

据我所知,如果可以避免,逻辑不应该知道GUI。我应该初始化逻辑类中的按钮而不是那种情况,如何?

1 个答案:

答案 0 :(得分:1)

使用Executive类的构造函数。添加

Executive(MainActivity activity){

m = activity;

}

使用MainActivity.java:25

public Executive exec = new Executive(this); 

使用Executive.java:21

MainActivity m =null;