线程阻塞了我的 Android UI

编程入门 行业动态 更新时间:2024-10-28 14:23:25
本文介绍了线程阻塞了我的 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

本文发布于:2023-06-07 22:47:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/572866.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   Android   UI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!