Android:为什么我会得到这些异步任务错误?

Android:为什么我会得到这些异步任务错误?,android,android-asynctask,thread-sleep,Android,Android Asynctask,Thread Sleep,我有一个asyncTaskProc,它从数据库中读取一些信息并将其写入ui 该代码在Android 4.0上运行良好,但在2.3上不起作用。。。 这是密码 新异步任务 `公共类IceCastPoll扩展TimerTask{ public IceCastPoll() { } @TargetApi(9) public void run() { new AsyncTaskProc().execute(); } }` 异步任务实现

我有一个asyncTaskProc,它从数据库中读取一些信息并将其写入ui

该代码在Android 4.0上运行良好,但在2.3上不起作用。。。 这是密码

新异步任务

`公共类IceCastPoll扩展TimerTask{

    public IceCastPoll() {

    }

    @TargetApi(9)
    public void run() {

        new AsyncTaskProc().execute();   
    }

}`
异步任务实现

@TargetApi(9)
class AsyncTaskProc extends AsyncTask<Void, String, Void> {
    List<Stream> streams=null;

    @Override
    protected void onPostExecute(Void result) {

        textSong =(TextView) findViewById(R.id.textViewCurrentSong);
        textArtist =(TextView) findViewById(R.id.textViewCurrentArtist);
        textTit=(TextView) findViewById(R.id.textViewTit);
        textArt=(TextView) findViewById(R.id.TextViewArt);
        copertina=(ImageView) findViewById(R.id.imageViewCopertina);
        new DownloadImageTask((ImageView) findViewById(R.id.imageViewCopertina)).execute("http://service.uniradiocesena.it/OnAir.jpg");

        try {
            for (Stream stream: streams) {

                try
                {   
                    //Thread.sleep(5000);
                    //textSong.setText((stream.getCurrentSong()));
                    textArt.setText("Artista:");
                    textTit.setText("Titolo:");
                    StringTokenizer tokens = new StringTokenizer(stream.getCurrentSong(), "-");
                    String first = tokens.nextToken();
                    String second = tokens.nextToken();
                    textSong.setText(first);
                    textArtist.setText(second);

                } catch (Exception e) { 
                    //Thread.sleep(5000);
                    textSong.setText((stream.getCurrentSong()));
                    textArt.setText("Rotazione:");
                    textTit.setText("");
                    textArtist.setText("");
                }
            }
        } catch (Exception e) {

            //Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_SHORT).show();

        }

    }

    @Override
    protected Void doInBackground(Void... unused) {



        Scraper scraper = new IceCastScraper();

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);


        try {
            streams = scraper.scrape(new URI("http://r35798.ovh.net:8000/"));

        } catch (ScrapeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            //Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            //Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show();
        }




        return (null);
    }


}`
@TargetApi(9)
类AsyncTaskProc扩展了AsyncTask{
列表流=空;
@凌驾
受保护的void onPostExecute(void结果){
textSong=(TextView)findViewById(R.id.textViewCurrentSong);
textArtister=(TextView)findViewById(R.id.textViewCurrentArtister);
textTit=(TextView)findViewById(R.id.textViewTit);
textArt=(TextView)findViewById(R.id.TextViewArt);
copertina=(ImageView)findViewById(R.id.imageViewCopertina);
新下载的ImageTask((ImageView)findViewById(R.id.imageViewCopertina))。执行(“http://service.uniradiocesena.it/OnAir.jpg");
试一试{
用于(流:流){
尝试
{   
//睡眠(5000);
//textSong.setText((stream.getCurrentSong());
textArt.setText(“Artista:”);
text tit.setText(“Titolo:”);
StringTokenizer令牌=新的StringTokenizer(stream.getCurrentSong(),“-”;
String first=tokens.nextToken();
String second=tokens.nextToken();
textSong.setText(第一);
textArtister.setText(第二);
}捕获(例外e){
//睡眠(5000);
textSong.setText((stream.getCurrentSong());
textArt.setText(“Rotazione:”);
text tit.setText(“”);
textArtister.setText(“”);
}
}
}捕获(例外e){
//Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_SHORT).show();
}
}
@凌驾
受保护的空位背景(空位…未使用){
刮冰器=新的刮冰器();
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
试一试{
streams=scraper.scrape(新URI(“http://r35798.ovh.net:8000/"));
}捕获(刮除异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
//Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}捕获(URISyntaxException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
//Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}
返回(空);
}
}`
LOGCAT错误

11-07 11:09:49.729: W/dalvikvm(18983): Exception Ljava/lang/RuntimeException; thrown while initializing Landroid/os/AsyncTask;
11-07 11:09:49.729: W/dalvikvm(18983): threadid=9: thread exiting with uncaught exception (group=0x40018560)
11-07 11:09:49.739: E/AndroidRuntime(18983): FATAL EXCEPTION: Timer-0
11-07 11:09:49.739: E/AndroidRuntime(18983): java.lang.ExceptionInInitializerError
11-07 11:09:49.739: E/AndroidRuntime(18983):    at com.example.appuniradiocesena.SwipeyTabsSampleActivity$IceCastPoll.run(SwipeyTabsSampleActivity.java:233)
11-07 11:09:49.739: E/AndroidRuntime(18983):    at     java.util.Timer$TimerImpl.run(Timer.java:284)
11-07 11:09:49.739: E/AndroidRuntime(18983): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11-07 11:09:49.739: E/AndroidRuntime(18983):    at android.os.Handler.<init>(Handler.java:121)
11-07 11:09:49.739: E/AndroidRuntime(18983):    at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
11-07 11:09:49.739: E/AndroidRuntime(18983):    at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
11-07 11:09:49.739: E/AndroidRuntime(18983):    at android.os.AsyncTask.<clinit>(AsyncTask.java:152)
11-07 11:09:49.739: E/AndroidRuntime(18983):    ... 2 more
11-07 11:09:49.729:W/dalvikvm(18983):异常Ljava/lang/RuntimeException;初始化Landroid/os/AsyncTask时抛出;
11-07 11:09:49.729:W/dalvikvm(18983):threadid=9:线程退出时出现未捕获异常(组=0x40018560)
11-07 11:09:49.739:E/AndroidRuntime(18983):致命异常:计时器-0
11-07 11:09:49.739:E/AndroidRuntime(18983):java.lang.ExceptionInInitializeError
11-07 11:09:49.739:E/AndroidRuntime(18983):在com.example.appuniradiocesena.SwipeyTabsSampleActivity$IceCastPoll.run(SwipeyTabsSampleActivity.java:233)
11-07 11:09:49.739:E/AndroidRuntime(18983):在java.util.Timer$TimerImpl.run(Timer.java:284)
11-07 11:09:49.739:E/AndroidRuntime(18983):原因:java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序
11-07 11:09:49.739:E/AndroidRuntime(18983):位于android.os.Handler.(Handler.java:121)
11-07 11:09:49.739:E/AndroidRuntime(18983):在android.os.AsyncTask$InternalHandler。(AsyncTask.java:421)
11-07 11:09:49.739:E/AndroidRuntime(18983):在android.os.AsyncTask$InternalHandler。(AsyncTask.java:421)
11-07 11:09:49.739:E/AndroidRuntime(18983):在android.os.AsyncTask.(AsyncTask.java:152)
11-07 11:09:49.739:E/AndroidRuntime(18983):…还有两个
任何建议都将非常感谢

英语错误的sry:D

 new AsyncTaskProc().execute();    
不要将此代码放在线程中

因为它已经是一个处理程序了


只要在需要的地方简单地执行代码,比如new AsyncTaskProc().Execute();我的朋友,在代码中有一个简单的建议就足够了:为什么每次都要执行
findViewById()
,因为您试图在特定的时间间隔执行AsyncTask,所以每次都不可行


相反,请在
onCreate()
方法中查找所有这些视图。

您应该使用在主线程上创建的方法启动AsyncTask。因此,请将IceCastPoll中的
run()
方法替换为以下方法:

private Handler handler = new Handler(Looper.getMainLooper());

@Override
public void run() {
   handler.post(new Runnable() {
      public void run() {
          new AsyncTaskProc().execute();
      }
   });
 }

你到底为什么要从线程执行异步任务?提个建议……我会照你说的做