本文介绍了线程阻塞了我的 Android UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 Android 应用程序中的 Java 线程有问题.我的嵌套线程阻塞了我的 UI,我该如何解决?
I have a problem with java Threads in my Android app. My nested thread blocks my UI, how can i resolve this?
MyClass.java
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 ;)
推荐答案我的嵌套线程阻塞了我的 UI
您需要使用 .start() 而不是 .run().即替换
You need to use .start() instead of .run(). That is, replace
Nested.run();与
Nested.start();(run 只是一个普通的方法.start() 是实际产生一个新线程的方法,它依次运行 run)
(run is just an ordinary method. start() is the method that actually spawns a new thread, which in turn runs run)
更多推荐
线程阻塞了我的 Android UI
发布评论