我的Android应用程序中的java Threads有问题.我的嵌套线程阻止了我的UI,我该如何解决这个问题呢?
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!!" );
}
}
}
}
这就是我运行它的方式:
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);
}
}
你可以帮帮我吗?我文字卡住;)
解决方法:
My nested thread blocks my UI
您需要使用.start()而不是.run().也就是说,替换
Nested.run();
同
Nested.start();
(run只是一个普通的方法.start()是实际生成一个新线程的方法,该线程反过来运行run)