1

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

 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 で例外が発生しています。私は混乱しています 。私を助けてください..

4

2 に答える 2

0

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

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

于 2012-01-20T11:04:32.607 に答える
0

asyc タスクの doinbackgound で UI を実行しているため、コードが例外をスローする必要があるためです。doingbackground メソッドからすべての UI 関連作業を削除してください。

于 2012-01-20T11:03:42.423 に答える