首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

android自己写的时间轮片算法错那里了?解决方案

2012-02-08 
android自己写的时间轮片算法错那里了?Java codepackage com.simeimport java.util.Randomimport androi

android自己写的时间轮片算法错那里了?

Java code
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:01.696: INFO/ActivityManager(68): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.sime/.PorActivity } from pid 139
11-20 05:19:02.026: INFO/ActivityManager(68): Start proc com.sime for activity com.sime/.PorActivity: pid=483 uid=10043 gids={}
11-20 05:19:04.106: INFO/ActivityManager(68): Displayed com.sime/.PorActivity: +2s108ms
11-20 05:19:08.145: DEBUG/dalvikvm(121): GC_EXTERNAL_ALLOC freed 24K, 44% free 3460K/6151K, external 6952K/7268K, paused 163ms
11-20 05:19:08.295: WARN/KeyCharacterMap(121): No keyboard for id 0
11-20 05:19:08.295: WARN/KeyCharacterMap(121): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
11-20 05:19:09.265: DEBUG/dalvikvm(139): GC_EXPLICIT freed 99K, 51% free 2971K/6023K, external 6214K/7646K, paused 82ms


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线程就可以了

热点排行