Java 使用单独的类时findViewById()不工作

Java 使用单独的类时findViewById()不工作,java,android,findviewbyid,Java,Android,Findviewbyid,嗨,我是Android新手 我想将以下代码与我的MainActivity文件分开保存。但是,当我试图在单独的类中查找dviewbyd()时,我得到了错误 “无法解决” 现在我知道我不能扩展MainActivity类,因为它会导致堆栈溢出,但是有人能告诉我如何从这个单独的文件访问textview吗 public class Counter { private TextView tempTextView; //Temporary TextView private Butto

嗨,我是Android新手

我想将以下代码与我的MainActivity文件分开保存。但是,当我试图在单独的类中查找dviewbyd()时,我得到了错误

“无法解决”

现在我知道我不能扩展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); }

    }
}
公共类计数器{
私有文本视图和文本视图;
//临时文本视图
专用按钮tempBtn;
公共处理程序mHandler=新处理程序();
公共长启动时间;
公共时间过长;
公共最终整数刷新率=100;
公共字符串小时、分钟、秒、毫秒;
公共长秒、分钟、小时、毫秒;
公共布尔停止=false;
public Runnable startTimer=new Runnable(){
公开募捐{
elapsedTime=System.currentTimeMillis()-startTime;
更新时间(elapsedTime);
mHandler.postDelayed(这是刷新率);
}
};
私有void updateTimer(浮动时间){
秒=(长)(时间/1000);
分钟=(长)((时间/1000)/60);
hrs=(长)((时间/1000)/60)/60);/*将秒转换为字符串*并设置格式,以确保在需要时有*前导零*/
秒=秒%60;
秒=字符串.valueOf(秒);
如果(秒=0){
秒数=“00”;
}
如果(秒<10&&secs>0){
秒=“0”+秒;
}/*将分钟数转换为字符串并格式化字符串*/
分钟=分钟%60;
分钟=字符串.valueOf(分钟);
如果(分钟==0){
分钟=“00”;
}
如果(
分钟<10和分钟>0
) {
分钟=“0”+分钟;
}/*将小时数转换为字符串并格式化字符串*/
小时=字符串的值(小时);
如果(小时==0){
小时数=“00”;
}
如果(小时数<10和小时数>0){
小时数=“0”+小时数;
}
/*虽然在本例中我们没有在计时器上使用毫秒,但我在事件中包含了您想要自己包含的代码*/
毫秒=String.valueOf((长)时间);
if(毫秒.length()==2){
毫秒=“0”+毫秒;
}

if(millizes.length()因为,
findViewById()
是活动类中的方法。这就是为什么不能在任何其他Java类中使用它

其次,您不能从任何工作线程更新Android应用程序UI

因此,最好使用并传递要在特定任务后更新的TextView引用


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

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

其次,您不能从任何工作线程更新Android应用程序UI

因此,最好使用并传递要在特定任务后更新的TextView引用


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

您需要展开视图,并从此视图调用mehod
findViewById()
。要展开视图,需要上下文(您可以在自定义构造函数中进行设置)


您需要膨胀视图并从此视图调用mehod
findViewById()
。要膨胀视图,您需要一个上下文(您可以在自定义构造函数中设置它)


您可以为
计数器
创建一个构造函数,该构造函数从
活动
中引用您的
文本视图

public Counter(TextView tv) {
    tempTextView = tv;
}
然后在实例化
计数器时,在
main活动上调用
findViewById()
,作为传入结果


您已经在UI线程上,不需要在此处执行
AsyncTask

您可以为
计数器
创建一个构造函数,从
活动
中引用您的
文本视图

public Counter(TextView tv) {
    tempTextView = tv;
}
然后在实例化
计数器时,在
main活动上调用
findViewById()
,作为传入结果


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

您可以使用一个构造函数来设置活动变量

public Counter (MainActivity activity) {
    mActivity = activity;
}
然后,你可以打电话

mActivity.findViewById(...);
它将在您在中设置的MainActivity布局中找到视图

setContentView(<layout>);
setContentView();

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

public Counter (MainActivity activity) {
    mActivity = activity;
}
然后,你可以打电话

mActivity.findViewById(...);
它将在您在中设置的MainActivity布局中找到视图

setContentView(<layout>);
setContentView();