2012-01-20 13 views
1

私は非常にアンドロイドです。私は2つの活動A、Bを得た。アクティビティAは、サーバーからデータを解析し、レベルを反復処理します。インテントを通してアクティビティBを呼び出す。アクティビティBでデータを表示するのに時間がかかるので、進行状況バーを表示しようとしています。ここに私のコードです。AsyncTaskプログレスバーを表示

public class Display extends Activity { 

     ProgressDialog dialog; 


     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.attributequestions); 

      new asynctask().execute(); 


     } 

     class asynctask extends AsyncTask<Context,Void,Void>{ 
      Survey[] surveyque=null; 
    // i hace created seperated class forsurvey that has info about data 
      String list[]; 


      private ProgressDialog Dialog; 
      @Override 
      protected void onPreExecute() 
      { 
       Dialog=ProgressDialog.show(Display.this, "Parsing Data", "Please wait.........."); 


      } 
      @Override 
      protected void onPostExecute(Void unused)  
      { 
       try 
       { 
        if(Dialog.isShowing()) 
        { 
         Dialog.dismiss(); 
        } 
        Intent intent=getIntent(); 

       } 
       catch(Exception e) 
       { 
    Log.d("Onsitev4", "error"); 
       } 
      } 

      @Override 
      protected Void doInBackground(Context... params) { 
       try { 

        LinearLayout layout1 = (LinearLayout) findViewById(R.id.linearLayout1); 

    //getting exception here. I dont understant why 
    // I have declared layout params and displaying activities in another class 

        ButtonView c = new ButtonView();     
        c.layout=layout1; 
        c.context =getBaseContext(); 


        DbCoreSqlSurveys surveys=new DbCoreSqlSurveys(getBaseContext()); 
        Document doc =surveys.getSurveySet(); 
        surveyquestions= GetSurveyLevels(doc,c); 


       } catch (TransformerFactoryConfigurationError e) { 
        e.printStackTrace(); 
       } 
       return null; 
      } 

     } 

     public SurveyObject[] GetSurveyLevels(Document doc, ButtonView c) { 
      NodeList nlQuestions = doc.getElementsByTagName("Survey"); 
      SurveyObject[] allsurveys = new SurveyObject[nlQuestions.getLength()]; 


      for (int i = 0; i < nlQuestions.getLength(); i++){ 

       Node survey = nlQuestions.item(i); 
       String f =survey.getNodeName(); 
       Log.d("OnsiteV4", "survey " + f); 

       NodeList surveyChildNodes = survey.getChildNodes(); 
       SurveyObject s=new SurveyObject(); 

       for (int j = 0; j < surveyChildNodes.getLength(); j++){ 

        Node surveyChild = surveyChildNodes.item(j);    
        String h =surveyChild.getNodeName(); 
        Log.d("OnsiteV4", "survey child node = " + h); 

        if (h !="#text"){ 
         Surveys t = Surveys.valueOf(h); 

         switch(t){ 
         case KeySurvey: 
          s.KeySurvey=surveyChild.getTextContent(); 
          displaySurveyLink(s.SurveyDescription,"",c,0,s.SurveyDescription,"","","",""); 
          break; 
         case SurveyDescription: 
          s.SurveyDescription=surveyChild.getTextContent(); 
          displaySurveyLink(s.SurveyDescription,"",c,0,s.SurveyDescription,"","","",""); 
          break; 
         case SurveyUserCode: 
          s.SurveyUserCode=surveyChild.getTextContent(); 
          break; 
         case Level1: 
          if(surveyChild.hasChildNodes()){ 
           s.Level1= processLevel1Nodes(surveyChild,c,s.SurveyDescription); 
          } 
          break; 
         default: 
          break; 
         } 
        } 

        allsurveys[i]=s; 
       } 
      } 

      return allsurveys; 
     } 

    // methods iterating through levels that is not showed 

     private void displaySurveyLink(final String description, String tag, ButtonView c, int indentation, final String surveyDescription, final String level1description, final String level2description, final String level3description, final String level4description) 
     { 
      if (description == null || tag == null){ 
       return; 
      } 
      final TextView tv = c.addButton(description,tag,indentation); 


      tv.setOnClickListener(new OnClickListener(){ 
       public void onClick(View v) { 

        final Intent intent = new Intent(); 
        intent.setClass(v.getContext(),ActivityB.class); 
        intent.putExtra("KeyLevel",tv.getTag().toString()); 

        intent.putExtra("SurveyDescription",surveyDescription); 
        intent.putExtra("level1description",level1description); 
        intent.putExtra("level2description",level2description); 
        intent.putExtra("level3description",level3description); 
        intent.putExtra("level4description",level4description); 
        intent.putExtra("Description",description); 

        if (tv.getTag() != null){ 
         if (tv.getTag().toString() != ""){ 
          startActivity(intent); 
         } 


        } 
       } 


      }); 
     } 
    } 

doinbackgroundで例外が発生しています。私は混乱しています 。助けてください..

+0

例外はありますか。あなたはlogcatを追加できますか?あなたの問題は、別のスレッド(aSyncTask)からUIスレッドを更新しようとしている可能性があると思います。 – SERPRO

+0

ur asynctaskで新しいインテントを開始する場合は、プログレスバーを閉じる必要があります。 Plsもあなたのエラーを投稿します。 – Mal

+0

E/AndroidRuntime(16904):java.lang.RuntimeException:java.util.concurrent.FutureTaskでdoInBackground()の実行中にエラーが発生しました$ Sync.innerSetException(FutureTask.java:273)...これは私の例外です – noname

答えて

0

UI以外のスレッドでUI要素にアクセスしているため、例外が発生しています。アプリケーションが作成するメインスレッドはUIスレッドです。これは、すべてのビジュアル要素が作成される場所で、アクセスする必要がある唯一のスレッドです。適切AsyncTaskを使用するには

、あなたはdoInBackgroundであなたの長時間実行の操作を実行して、あなたはUI(表示/非表示の進捗ダイアログ、更新ビュー、など)で動作するようにonPreExecuteonPostExecuteonProgressUpdatedを使用しています。 AsyncTaskを使用し、進捗状況を表示したい場合は、onProgressUpdatedをオーバーライドして、パラメータ型Integerを与え、doInBackgroundからpublishProgressを呼び出します。これには、基本クラス署名をAsyncTask<Context,Void,Void>からAsyncTask<Context,Integer,Void>に変更する必要があります。これには他のオブジェクト型を使用することもできます。たとえば、完了したタスクの割合を表示する場合はIntegerを例として使用します。

+0

これはUIの操作はここで説明することができます頼むために愚かかもしれない、ここで。私はバックグラウンドで実行する必要があるデータを解析しようとしていますか? – noname

+0

"UI"と言うとき、私はあなたのViewオブジェクト(Button、LinearLayout、TextView)を参照しています。これらは、画面に表示されるメインスレッド上に作成されます。これはUIスレッドとして知られています。このスレッドはまた、ユーザの入力を聞いて処理すること(画面に触れる、ボタンを押すなど)を担当する。バックグラウンドスレッドでdbをダウンロードしたり作業したりするなどの長い作業を行う理由は、UIスレッドが常に画面を更新したりユーザーの入力に応答できるようにするためです – Rich

+0

あなたのフィードバックに感謝します – noname

0

asycタスクのdoinbackgoundでUIのものをやっているので、あなたのコードが例外をスローするはずです。すべてのUI関連の作業を、backbackgoundメソッドから削除してください。

+0

を取得している私はビットを明確に説明する方法を変更することができます。私はupdateprogressを追加する必要があることを知っているが、私は少し混乱して追加する。申し訳ありません – noname

関連する問題