2015-03-02 56 views
1

您好我是新的時候到AndroidfindViewById()使用一個單獨的類

我想從我的MainActivity文件seperatly繼續下面的代碼無法正常工作。然而,當我嘗試findViewById()在單獨的類中,我得到的錯誤

「解決不了」

現在我知道我不能擴展在MainActivity類,因爲它會導致棧溢出,但有人可以告訴我如何去從這個單獨的文件訪問文本視圖?

public class Counter { 

    private TextView tempTextView; 
    //Temporary TextView 
    private Button tempBtn; 
    public Handler mHandler = new Handler(); 
    public long startTime; 
    public long elapsedTime; 
    public final int REFRESH_RATE = 100; 
    public String hours,minutes,seconds,milliseconds; 
    public long secs,mins,hrs,msecs; 
    public boolean stopped = false; 

    public Runnable startTimer = new Runnable() { 
     public void run() { 
      elapsedTime = System.currentTimeMillis() - startTime; 
      updateTimer(elapsedTime); 
      mHandler.postDelayed(this,REFRESH_RATE); 
     } 
    }; 


    private void updateTimer (float time) { 
     secs = (long) (time/1000); 
     mins = (long) ((time/1000)/60); 
     hrs = (long) (((time/1000)/60)/60); /* Convert the seconds to String * and format to ensure it has * a leading zero when required */ 
     secs = secs % 60; 
     seconds = String.valueOf(secs); 
     if (secs == 0) { 
      seconds = "00"; 
     } 
     if (secs < 10 && secs > 0) { 
      seconds = "0" + seconds; 
     } /* Convert the minutes to String and format the String */ 
     mins = mins % 60; 
     minutes = String.valueOf(mins); 
     if (mins == 0) { 
      minutes = "00"; 
     } 
     if (
       mins < 10 && mins > 0 
       ) { 
      minutes = "0" + minutes; 
     } /* Convert the hours to String and format the String */ 
     hours = String.valueOf(hrs); 
     if (hrs == 0) { 
      hours = "00"; 
     } 
     if (hrs < 10 && hrs > 0) { 
      hours = "0" + hours; 
     } 
     /* Although we are not using milliseconds on the timer in this example * I included the code in the event that you wanted to include it on your own */ 
     milliseconds = String.valueOf((long) time); 
     if (milliseconds.length() == 2) { 
      milliseconds = "0" + milliseconds; 
     } 
     if (milliseconds.length() <= 1) { 
      milliseconds = "00"; 
     } 
     milliseconds = milliseconds.substring(milliseconds.length() - 3, milliseconds.length() - 2); 
     /* Setting the timer text to the elapsed time */ 
     // ((TextView) findViewById(R.id.elapsed_value)).setText(hours + ":" + minutes + ":" + seconds); 
     // ((TextView)findViewById(R.id.timerMs)).setText("." + milliseconds); } 

    } 
} 

回答

1

因爲findViewById()是Activity類的方法。這就是爲什麼你不能在任何其他Java類中使用它。

第二,您無法從任何工作線程更新Android應用程序UI。

所以最好使用AsyncTask並傳遞特定任務後要更新的TextView引用。

如果您要從其他工作線程更新應用程序UI,請使用runOnMainUiThread()。但請確保runOnUiThread()僅適用於活動上下文。

2

你需要膨脹一個視圖,並從這個視圖調用mehod findViewById()。膨脹視圖,你需要一個上下文(你可以將其設置在一個自定義的構造函數)

View view; 
LayoutInflater inflater = (LayoutInflater) yourContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.mylayout, null); 

tempTextView = (TextView) view.findViewById(R.id.tv); 
1

您可以爲Counter一個構造函數從ActivityTextView參考。

public Counter(TextView tv) { 
    tempTextView = tv; 
} 

然後在您的MainActivity致電findViewById()爲合格的結果,當你實例Counter

你已經在UI線程上,沒有必要AsyncTask這裏...

1

您可以在其中設置的活動變量的構造函數。

public Counter (MainActivity activity) { 
    mActivity = activity; 
} 

然後,您可以撥打

mActivity.findViewById(...); 

它會發現,你在

setContentView(<layout>); 
設置在MainActivity佈局視圖