2012-08-25 39 views
0

我創建了一個將以二進制形式打印出時間的應用程序。 這個邏輯工作的很好,但是當我試圖分離邏輯和GUI時,遇到了一些問題。似乎有一個交叉引用問題,但我似乎無法找到它。從另一個類引用MainActivity中的RadioButton時出錯

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

你真的應該改進這個問題。提出幾百行代碼並期望有人調試所有這些代碼,這不太可能讓你得到答案。如果您能夠劃定您的代碼的哪一部分有問題並僅提供該部分會更好。 – SJuan76

回答

1

使用Executive類的構造函數。添加

Executive(MainActivity activity){ 

m = activity; 

} 

使用MainActivity.java:25

public Executive exec = new Executive(this); 

使用Executive.java:21

MainActivity m =null; 
+0

有趣!然而,沒有工作,在m的第一個引用處返回nullpointerexception。 – hacke

+0

也許在你的代碼中存在其他問題。移動新的執行官(這);活動的開展。繼續更新代碼,並在更改ti時更新代碼 – nandeesh

相關問題