这是我的活动代码
public class HomeActivity extends Activity {
private String html = "";
private Handler mHandler;
private ActionBar actionBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
actionBar = (ActionBar) findViewById(R.id.actionbar);
actionBar.setHomeAction(new IntentAction(this, createIntent(this), R.drawable.ic_title_home_default));
actionBar.setTitle("someTitle");
Intent intent = new Intent(this, ComposeActivity.class);
final Action otherAction = new IntentAction(this, intent, R.drawable.ic_title_share_default);
actionBar.addAction(otherAction);
mHandler = new Handler();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus)
checkPrice.start();
}
public static Intent createIntent(Context context) {
Intent i = new Intent(context, HomeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
return i;
}
private Thread checkPrice = new Thread() {
public void run() {
actionBar.setProgressBarVisibility(View.VISIBLE);
try {
//...some code
mHandler.post(showUpdate);
} catch (Exception e) {
}
}
};
private Runnable showUpdate = new Runnable(){
public void run(){
actionBar.setProgressBarVisibility(View.GONE);
Toast.makeText(HomeActivity.this, "HTML Code: " + html, Toast.LENGTH_SHORT).show();
}
};
}
添加onWindowFocusChanged
方法后,意图intent
不能再开始了。我收到以下错误:
12-12 10:50:02.468: W/dalvikvm(540): threadid=3: thread exiting with
uncaught exception (group=0x4001b188) 12-12 10:50:02.468:
E/AndroidRuntime(540): Uncaught handler: thread main exiting due to
uncaught exception 12-12 10:50:02.468: E/AndroidRuntime(540):
java.lang.IllegalThreadStateException: Thread already started. 12-12
10:50:02.468: E/AndroidRuntime(540): at
java.lang.Thread.start(Thread.java:1322) 12-12 10:50:02.468:
E/AndroidRuntime(540): at
HomeActivity.onWindowFocusChanged(HomeActivity.java:58)
12-12 10:50:02.468: E/AndroidRuntime(540): at
com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:1969)
12-12 10:50:02.468: E/AndroidRuntime(540): at
android.view.View.dispatchWindowFocusChanged(View.java:3731) 12-12
10:50:02.468: E/AndroidRuntime(540): at
android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:657)
12-12 10:50:02.468: E/AndroidRuntime(540): at
android.view.ViewRoot.handleMessage(ViewRoot.java:1819) 12-12
10:50:02.468: E/AndroidRuntime(540): at
android.os.Handler.dispatchMessage(Handler.java:99) 12-12
10:50:02.468: E/AndroidRuntime(540): at
android.os.Looper.loop(Looper.java:123) 12-12 10:50:02.468:
E/AndroidRuntime(540): at
android.app.ActivityThread.main(ActivityThread.java:4363) 12-12
10:50:02.468: E/AndroidRuntime(540): at
java.lang.reflect.Method.invokeNative(Native Method) 12-12
10:50:02.468: E/AndroidRuntime(540): at
java.lang.reflect.Method.invoke(Method.java:521) 12-12 10:50:02.468:
E/AndroidRuntime(540): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-12 10:50:02.468: E/AndroidRuntime(540): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 12-12
10:50:02.468: E/AndroidRuntime(540): at
dalvik.system.NativeStart.main(Native Method)
任何想法如何解决这个问题?
答案 0 :(得分:4)
你的onWindowFocusChanged对同一个不正确的线程多次调用Thread.start()
。您可能希望使用isStarted
之类的变量来保证Thread.start()
仅被调用一次。或者每次都开始一个新的主题。这取决于你的程序的逻辑。
答案 1 :(得分:0)
试试这个
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus && !checkPrice.isAlive())
checkPrice.start();
}