2012-11-07 8 views
5

나는 코드가 안드로이드 4.0 perfectcly 작동하지만 2.3에서 작동하지 않습니다 ... 을 여기에 코드Android : 왜 이러한 AsyncTask 오류가 발생합니까?

입니다

DB를 일부 정보를 읽고 UI에 그것을 쓰 asyncTaskProc을 가지고 ...

NEW AsyncTask를

`공용 클래스 IceCastPoll은 TimerTask를 확장 {

public IceCastPoll() { 

    } 

    @TargetApi(9) 
    public void run() { 

     new AsyncTaskProc().execute(); 
    } 

}` 

AsyncTask를 구현

,451,515,
@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); 
    } 


}` 

로그 캣 오류

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 

이 매우 매우 apreciated 될 것입니다 어떤 제안! 영어 실수

및 SRY : D

+0

왜 당신이 스레드에서 AsyncTask를 실행할까요? – njzk2

답변

9

주 스레드에서 생성 된 Handler으로 AsyncTask를 시작해야합니다. 그래서 하나 IceCastPoll에 run() 방법을 대체 : 지구에

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

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

awsome ... 그게 완벽하게 작동합니다! 타이 남자! – radudac

1
new AsyncTaskProc().execute();  

망가 스레드 내에서이 코드를 넣어.

이미 처리기이기 때문에.

Simple 새로운 AsyncTaskProc()과 같은 코드를 실행하십시오. execute(); 그만큼 내 친구

0

코드에서 하나의 빠른 제안 : 매번 findViewById()을 수행하고 있습니다. AsyncTask를 특정 간격으로 실행하려고 시도 할 때마다 매번 그것을 수행 할 수 없습니다.

대신 onCreate() 방법 내의 모든보기를 찾으십시오.

+0

ty for suggestion ... 나는 당신이 말한 것처럼 할 것입니다. – radudac

관련 문제