Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 启动新线程时的黑色视图_Java_Android_Multithreading - Fatal编程技术网

Java 启动新线程时的黑色视图

Java 启动新线程时的黑色视图,java,android,multithreading,Java,Android,Multithreading,在我的android应用程序中,我希望每60秒自动刷新一次。所以我试着这样做: public void refresh_check() { Thread myThread = new Thread() { int counter = 0; @Override public void run() { MyActivity.this.runOnUiThread(ne

在我的android应用程序中,我希望每60秒自动刷新一次。所以我试着这样做:

public void refresh_check() {
        Thread myThread = new Thread()
        {
            int counter = 0;
            @Override
            public void run() {
                MyActivity.this.runOnUiThread(new Runnable(){
                    @Override
                    public void run() {
                        while (counter < 60) {
                            try {
                                Thread.sleep(1000);
                                counter += 1;
                                System.out.println("Counter: " + counter);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                        refresh();
                    }});
                super.run();
            }
        };
        myThread.start();       
    }
public void refresh\u check(){
线程myThread=新线程()
{
int计数器=0;
@凌驾
公开募捐{
MyActivity.this.rununuithread(新的Runnable(){
@凌驾
公开募捐{
同时(计数器<60){
试一试{
睡眠(1000);
计数器+=1;
系统输出打印项次(“计数器:+计数器);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
刷新();
}});
super.run();
}
};
myThread.start();
}

它的工作方式是将计数器打印到logcat中,但在我的应用程序中,我得到一个黑色视图
refresh()
只是一个带有http请求的函数,它是单独工作的,因此错误必须在任何地方的线程中:/Can someone help?

您没有正确使用
线程。在UI线程上运行长任务就像根本不使用
线程一样。要完成您需要的任务,您应该这样做:

public void refresh_check() {
        Thread myThread = new Thread()
        {
            int counter = 0;
            @Override
            public void run() {
                while (counter < 60) {
                            try {
                                Thread.sleep(1000);
                                counter += 1;
                                System.out.println("Counter: " + counter); //I think this may cause exception, if it does try removing it
                            } catch (InterruptedException e) { 
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                        refresh(); // In refresh(), use TextView.post(runnable) to post update the TextView from outside the UI thread or use handlers
                    }});
                super.run();
            };
        myThread.start();       
    }
public void refresh\u check(){
线程myThread=新线程()
{
int计数器=0;
@凌驾
公开募捐{
同时(计数器<60){
试一试{
睡眠(1000);
计数器+=1;
System.out.println(“Counter:+Counter);//我认为这可能会导致异常,如果它确实尝试删除它的话
}捕获(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
refresh();//在refresh()中,使用TextView.post(runnable)从UI线程外部发布更新TextView或使用处理程序
}});
super.run();
};
myThread.start();
}

另外,看看AsyncTask类,它使您能够在UI线程之外运行长任务(
doInBackground()
),并使用(
onPostExecute()
)的结果更新UI。

为什么要在UI线程上运行它?这将阻止操作。因为刷新函数调用了其他一些更新TextView的函数,所以您可以使用TextView.post(Runnable)从UI线程外部更新TextView。或者,您可以使用AsyncTask在doInBackground()中进行处理,然后在onPostExecute()中使用我现在得到的结果更新TextView。是由一位训练员做的,谢谢你的帮助