方法setText必须从UI线程中调用

方法setText必须从UI线程中调用

本文介绍了方法setText必须从UI线程中调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已在stackoverflow上读取了所有设置并从UI线程调用了该设置,但似乎没有任何关联.该代码在2年前有效,最近已将其打开.并显示此错误. 必须从UI线程调用方法setText".
哪些更新正在执行此操作?用注释标记setText行,使其更清晰.有任何建议吗?

Have read all the set and get called from the UI thread, on stackoverflow but none seems relevent. this code worked 2years ago, recently opened it. and it shows this error. "Method setText must be called from the UI thread".
What updates are doing this?marked the setText lines with comments to make it more clear. Any sugestions?

public class MainActivity extends Activity {

    static final String baseURL = "http://api.yr.no/weatherapi/locationforecast/1.9/?lat=";
TextView dato, grader, vindhastighet, vindretning;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainactivity);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .detectAll().penaltyLog().build();     StrictMode.setThreadPolicy(policy);


        SmartLocation.with(this).location()
                .oneFix()
                .start(new OnLocationUpdatedListener() {
                    @Override
                    public void onLocationUpdated(Location location) {

                        dato = (TextView)findViewById(R.id.gpsbydato_txt);
                        grader = (TextView)findViewById(R.id.gpsbygrader_txt);
                        vindhastighet = (TextView)findViewById(R.id.gpsbyvindhastighet_txt);
                        vindretning = (TextView)findViewById(R.id.gpsbyvindretning_txt);

                        Double latitude = location.getLatitude();
                        Double longtitude = location.getLongitude();

                        StringBuilder Url = new StringBuilder(baseURL);
                        Url.append(latitude + ";lon=" + longtitude);
                        final String fullUrl = Url.toString();


                        class Read extends AsyncTask<String, Integer, String> {
                            @Override
                            protected String doInBackground(String... arg0) {

                                try {
                                    URL website = new URL(fullUrl);
                                //xmlreader parser data
                                    SAXParserFactory spf = SAXParserFactory.newInstance();
                                    SAXParser sp = spf.newSAXParser();
                                    XMLReader xr = sp.getXMLReader();
                                    HandlingXMLStuff doingWork = new HandlingXMLStuff();
                                    xr.setContentHandler(doingWork);
                                    xr.parse(new InputSource(website.openStream()));

                                    String informationTemp = doingWork.getInformationTemp();
                                    String informationVindretning = doingWork.getInformationVindretning();
                                    String informationVindhastighet = doingWork.getInformationVindhastighet();
//grader.setText is causing the error.
                                    grader.setText(informationTemp);
//vindhastighet.setText is causing the error.

                                    vindhastighet.setText(informationVindhastighet);
//vindretning.setText is causing the error.

                                    vindretning.setText(informationVindretning);

                                } catch (Exception e) {
                                    Log.e("URL:", fullUrl);
                                    dato.setText("erroorrr"); //also this setText
                                    System.out.println(e);
                                }


                                return null;
                            }

                            @Override
                            protected void onPostExecute(String result) {
                                dato.setText(result);
                                super.onPostExecute(result);
                            }
                        }

                        }

    });
}
}

推荐答案

您无法从AsyncTask中的doInBackground更新UI.

You can not update UI from doInBackground in AsyncTask.

所有与UI相关的操作都需要在UI线程内完成.

All UI related operations need to be done inside UI thread.

您可以使用runonUithreaddoInBackground

这是一个示例

代码:

  @Override
protected String doInBackground(String... arg0) {

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            grader.setText(informationTemp);
            vindhastighet.setText(informationVindhastighet);
            vindretning.setText(informationVindretning);

        }
    });

}

或者..更好的解决方案是将setText()代码移到onPostExecute()

Or..better solution is to move your setText() code inside onPostExecute()

这篇关于方法setText必须从UI线程中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:42