findViewById()在使用单独的类时不起作用

时间:2015-03-02 22:58:00

标签: java android findviewbyid

您好我是Android新手

我想从MainActivity文件中单独保留以下代码。但是,当我尝试在单独的类中查找ViewById()时,我收到错误

  

"无法解决"

现在我知道我无法扩展MainActivity类,因为它会导致堆栈溢出,但是有人可以告诉我如何从这个单独的文件中访问textview吗?

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); }

    }
}

4 个答案:

答案 0 :(得分:2)

您需要对视图进行充气并从此视图中调用方法findViewById()。要扩展视图,您需要一个Context(您可以在自定义构造函数中设置它)

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 :(得分:1)

因为findViewById()是Activity类的方法。这就是为什么你不能在任何其他Java类中使用它。

其次,您无法从任何工作线程更新Android应用程序UI。

最好使用AsyncTask并在特定任务之后传递要更新的TextView引用。

如果要从其他工作线程更新Application UI,请使用runOnMainUiThread()。但请确保runOnUiThread()仅适用于活动上下文。

答案 2 :(得分:1)

您可以为Counter创建一个构造函数,该构造函数会引用TextView中的Activity

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

当您findViewById()实例化时,请在MainActivity上致电Counter作为结果传递。

您已经在UI线程上,这里不需要AsyncTask ...

答案 3 :(得分:1)

您可以使用构造函数设置活动变量。

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

然后,您可以致电

mActivity.findViewById(...);

将在您在

中设置的MainActivity布局中找到该视图
setContentView(<layout>);