我创建了一个应用程序,它将以二进制形式打印出时间。逻辑工作得很好,但是当我尝试将逻辑和 GUI 分开时,我遇到了一些问题。似乎有一个交叉引用问题,但我似乎无法找到它。
主要活动:
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。我应该初始化逻辑类中的按钮吗?在这种情况下,如何初始化?