android自己写的时间轮片算法错那里了?
package com.sime;import java.util.Random;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.text.Layout;import android.view.ViewGroup.LayoutParams;import android.widget.LinearLayout;import android.widget.ProgressBar;import android.widget.TextView;import android.widget.Toast;public class RunscActivity extends Activity { private int jcbt; private int cput; private int timeout=0; private int s=0; LinearLayout layout; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.runnables); layout = (LinearLayout) findViewById(R.id.layoutBackground); jcbt= this.getIntent().getExtras().getInt("jcbtnumber"); cput=this.getIntent().getExtras().getInt("cputimes"); final ProgressBar a[]=new ProgressBar[10]; final int b[]={10,20,30,40}; final Runnable uirun[]=new Runnable[10]; for(int i=0;i<jcbt;i++) { ProgressBar bar = new ProgressBar(this,null,android.R.attr.progressBarStyleHorizontal); layout.addView(bar, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); Runnable callback= new Runnable() { @Override public void run() { // TODO Auto-generated method stub a[s++].incrementProgressBy(b[s++]); } }; a[i]=bar; uirun[i]=callback; } final Handler handler=new Handler(); final Thread thread=new Thread() { @Override public void run() { try { for(int m=0;m<10;m++) { Toast.makeText(getApplicationContext(), "第"+(m+1)+"次轮转", Toast.LENGTH_SHORT).show(); for(int i=0;i<jcbt;i++) { Thread.sleep(1000); handler.post(uirun[i]); Toast.makeText(getApplicationContext(), "执行进程"+i, Toast.LENGTH_SHORT).show(); } } } catch (InterruptedException e){ // TODO Auto-generated catch block e.printStackTrace(); } } }; thread.start(); } }
11-20 05:19:45.875: INFO/ActivityManager(68): Starting: Intent { cmp=com.sime/.RunscActivity (has extras) } from pid 483
11-20 05:19:46.037: WARN/dalvikvm(483): threadid=10: thread exiting with uncaught exception (group=0x40015560)
11-20 05:19:46.037: ERROR/AndroidRuntime(483): FATAL EXCEPTION: Thread-11
11-20 05:19:46.037: ERROR/AndroidRuntime(483): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11-20 05:19:46.037: ERROR/AndroidRuntime(483): at android.os.Handler.<init>(Handler.java:121)
11-20 05:19:46.037: ERROR/AndroidRuntime(483): at android.widget.Toast.<init>(Toast.java:68)
11-20 05:19:46.037: ERROR/AndroidRuntime(483): at android.widget.Toast.makeText(Toast.java:231)
11-20 05:19:46.037: ERROR/AndroidRuntime(483): at com.sime.RunscActivity$2.run(RunscActivity.java:136)
11-20 05:19:46.085: WARN/ActivityManager(68): Force finishing activity com.sime/.RunscActivity
11-20 05:19:47.945: INFO/ActivityManager(68): Displayed com.sime/.RunscActivity: +2s11ms
11-20 05:19:49.725: INFO/Process(483): Sending signal. PID: 483 SIG: 9
11-20 05:19:51.575: INFO/ActivityManager(68): Process com.sime (pid 483) has died.
错在何处呢?jcbt和cput都是我从另一个界面传过来的整数
[解决办法]
Android中不能在子线程中来刷新UI线程:
你把 Toast.makeText的调用放在UI线程就可以了