1

我对安卓很陌生。我有两个活动 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

这是因为当您在 asyc 任务的 doinbackgound 中执行 UI 操作时,您的代码应该抛出异常。请从doingbackgound 方法中删除所有与UI 相关的工作。

于 2012-01-20T11:03:42.423 回答
0

由于您正在非 UI 线程上访问 UI 元素,因此您遇到了异常。应用程序创建的主线程是 UI 线程,这是创建所有视觉元素的地方,因此是您应该访问它们的唯一线程。

To appropriately use AsyncTask, you run your long-running operations in doInBackground, and you use onPreExecute, onPostExecute and onProgressUpdated to work with the UI (show/hide progress dialogs, update views, etc). Whenever I use an AsyncTask and I want to show progress, I override onProgressUpdated giving it parameter type Integer and I call publishProgress from doInBackground. This would require a change of the base class signature from AsyncTask<Context,Void,Void> to AsyncTask<Context,Integer,Void>. You can use other object types for this as well...I just use Integer as an example if you want to show the percentage of the task that is complete, for example.

于 2012-01-20T11:04:32.607 回答