线程块我的Andr​​oid UI [英] Thread blocks my Android UI

查看:128
本文介绍了线程块我的Andr​​oid UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Java线程的问题在我的Andr​​oid应用程序。我嵌套线程块我的用户界面,我该如何解决?

MyClass.java

 包com.knobik.gadu;

进口android.util.Log;

公共MyClass类{

    公共无效StartTheThread(){

        螺纹嵌套=新主题(新NestedThread());
        Nested.run();
    }

    私有类NestedThread实现Runnable {

        公共无效的run(){

            而(真){
                Log.d(DUPA!,调试日志垃圾邮件!);
            }

        }

    }

}
 

和我就是这样运行:

 包com.knobik.gadu;

进口java.io.IOException异常;

进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.ResponseHandler;
进口org.apache.http.client.methods.HttpGet;
进口org.apache.http.impl.client.BasicResponseHandler;
进口org.apache.http.impl.client.DefaultHttpClient;

进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;

公共类AndroidGadu延伸活动​​{
    公共静态最后弦乐LogAct =AndroidGadu;

    公共无效OnClickTest(视图v){

        MyClass的测试=新MyClass的();
        test.StartTheThread();

    }


    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);



    }
}
 

你能帮助我吗?我literaly卡住;)

解决方案
  

我的嵌套线程块我的UI

您需要使用。开始(),而不是的.RUN()。也就是说,替换

  Nested.run();
 

  Nested.start();
 

运行只是一个普通的方法。启动()是实际产生一个新的线程的方法,这反过来又运行运行

I have a problem with java Threads in my Android app. My nested thread blocks my UI, how can i resolve this?

MyClass.java

package com.knobik.gadu;

import android.util.Log;

public class MyClass {

    public void StartTheThread() {

        Thread Nested = new Thread( new NestedThread() );
        Nested.run();
    }

    private class NestedThread implements Runnable {

        public void run() {

            while (true) {
                Log.d( "DUPA!", "debug log SPAM!!" );
            }

        }

    }

}

and this is how i run it:

package com.knobik.gadu;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class AndroidGadu extends Activity {
    public static final String LogAct = "AndroidGadu";

    public void OnClickTest(View v) {

        MyClass test = new MyClass();
        test.StartTheThread();

    }


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



    }
}

Could you help me? I'm literaly stuck ;)

解决方案

My nested thread blocks my UI

You need to use .start() instead of .run(). That is, replace

Nested.run();

with

Nested.start();

(run is just an ordinary method. start() is the method that actually spawns a new thread, which in turn runs run)

这篇关于线程块我的Andr​​oid UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆