2012-10-25 4 views
1

나는 기본 클래스와 기본 클래스를 확장하는 하위 클래스가 있습니다. 수퍼 클래스에는 일부 작업을 수행하는 비동기 작업이 있습니다. 나는 그렇지가 IllegalInitializerError을 발생하기 때문에 UI 스레드에서 그것을 실행하여이 전화 : 내 비동기 작업에서는ProgressBar가 바로 android에 표시되지 않습니다.

superclass.this.runOnUiThread(new Runnable() { 
    public void run() { 
     String p=""; 
     try { 
      p=new asynctasker().execute().get(); 
     } 
    } 
} 

: 대화가 거의 요청의 끝 부분에 표시됩니다 그러나

protected void onPreExecute() 
{ 
    // TODO Auto-generated method stub 
    super.onPreExecute(); 
    //showDialog(); 
    Log.d("Now","Inside right now"); 
    dialog = ProgressDialog.show(class_create_event.this, "Loading1", "Please Wait"); 
} 

. 나는 부분적으로 정확하게 인쇄되어있다. 뭔가가 내 UI 스레드를 차단하는 것을 압니다. 하지만 UI 스레드에서 비동기 작업을 호출하지 않으면 잘못된 이니셜 라이저 오류가 발생합니다. 출구가 있습니까?

답변

1

이 비동기 작업이 난 그냥 직접 호출 할 수 없습니다 슈퍼 클래스의 컨텍스트에서 실행하기 때문에이 방법처럼

FetchRSSFeeds async = new FetchRSSFeeds(); 
async.execute(); 


private class FetchRSSFeeds extends AsyncTask<String, Void, Boolean> { 

    private ProgressDialog dialog = new ProgressDialog(HomeActivity.this); 

    /** progress dialog to show user that the backup is processing. */ 
    /** application context. */ 

    protected void onPreExecute() { 
     this.dialog.setMessage(getResources().getString(
       R.string.Loading_String)); 
     this.dialog.show(); 
    } 

    protected Boolean doInBackground(final String... args) { 
     try { 

      // Fetch the RSS Feeds from URL 
      // do background process 

      return true; 
     } catch (Exception e) { 
      Log.e("tag", "error", e); 
      return false; 
     } 
    } 

    @Override 
    protected void onPostExecute(final Boolean success) { 

     if (dialog.isShowing()) { 
      dialog.dismiss(); 
     } 

     if (success) { 
      // Setting data to list adaptar 
      setListData(); 
     } 
    } 
} 
+0

을 AsyncTask를

전화를 호출하기위한 UIthread을 가질 필요가 없습니다. 이 함수를 호출하면 Exception Ljava/lang/RuntimeException; Landroid/os/AsyncTask 중에 발생합니다. . 메인 애플리케이션 스레드에서만 AsyncTask 인스턴스를 생성하고 실행할 수있다. –

관련 문제