从其他类引用mainactivity中的radiobutton时出错

iovurdzv  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(325)

我创建了一个应用程序,将打印出二进制时间。逻辑工作得很好,但是当我尝试分离逻辑和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);

日志摘录:

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。我应该初始化逻辑类中的按钮吗?在这种情况下,如何初始化?

zqdjd7g9

zqdjd7g91#

为执行类使用构造函数。添加

Executive(MainActivity activity){

m = activity;

}

使用mainactivity。java:25

public Executive exec = new Executive(this);

使用executive。java:21

MainActivity m =null;

相关问题