0

嗨,我的 ListView 上的线程有一些问题。当我第一次运行活动时,线程不起作用,但如果我返回并再次运行该活动,一切正常。

那是我的代码:

protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getListView().setOnItemClickListener(listlistener);
        // Find the ListView resource.   
        raid_list = new ArrayList<RaidWpis>();
        this.m_adapter = new OrderAdapter(this, R.layout.row, raid_list);
        setListAdapter(this.m_adapter);

        viewOrders = new Runnable(){
            @Override
            public void run() {
                getOrders();
            }
        };
        Thread thread =  new Thread(null, viewOrders, "raid");
        thread.start();
        m_ProgressDialog = ProgressDialog.show(Raidy_lista.this,"Prosze czekac...", "Pobieram dane ...", true);


        //ładowanie raidów z serwera
        tv  = (TextView)findViewById(R.id.tresc_gry);
        text    = "brak odpowiedzi";
        tv.setText("wysyłam plik");

        /*
        try {
            postData();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        */

    }

private void getOrders() {

        tv  = (TextView)findViewById(R.id.tresc_gry);
            try{
                raid_list = new ArrayList<RaidWpis>();

                // Create a new HttpClient and Post Header

                SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
                if (sharedPrefs.getString("ustawienia_wybrana_gra","1").equals("1"))
                {
                    user_login=sharedPrefs.getString("ustawienia_gra_1_login","1");
                    user_haslo=sharedPrefs.getString("ustawienia_gra_1_haslo","1");
                    user_adres=getString(R.string.ustawienia_gra_1_adres);
                }
                else
                {
                    user_login=sharedPrefs.getString("ustawienia_gra_2_login","1");
                    user_haslo=sharedPrefs.getString("ustawienia_gra_2_haslo","1");
                    user_adres=getString(R.string.ustawienia_gra_2_adres);
                }
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(user_adres+"/raidplanner_android.php");
                JSONObject json = new JSONObject();
                //tv.append("\nŁącza z serwerem");
                try {
                    // JSON data:
                    tv.append("\nWysyłam dane");
                    tv.append("\nStrona:" + user_adres);
                    tv.append("\nLogin:" + user_login);
                    tv.append("\nHasło:" + user_haslo);
                    json.put("metoda", "pokaz_raidplanner");
                    json.put("login", user_login);
                    json.put("haslo", user_haslo);
                    json.put("strona", user_adres);

                    JSONArray postjson=new JSONArray();
                    postjson.put(json);

                    // Post the data:
                    httppost.setHeader("json",json.toString());
                    httppost.getParams().setParameter("jsonpost",postjson);

                    // Execute HTTP Post Request
                    //System.out.print(json);
                    //httppost.setEntity(new UrlEncodedFormEntity(json));
                    HttpResponse response = httpclient.execute(httppost);
                    tv.append("\nOdbieram dane");
                    // for JSON:
                    if(response != null)
                    {
                        tv.append("\nDane odebrane\n\n");
                        InputStream is = response.getEntity().getContent();

                        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                        StringBuilder sb = new StringBuilder();

                        String line = null;
                        try {
                            while ((line = reader.readLine()) != null) {
                                sb.append(line + "\n");
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                            tv.append("\nBłąd IO");
                        } finally {
                            try {
                                is.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        text = sb.toString();
                    }
                    //tv.append(text);
                    JSONObject entireJson = new JSONObject(text);
                    //String odpowiedz = entireJson.getString("odpowiedz");
                    JSONObject raidyObject = entireJson.getJSONObject("raidy");
                    JSONArray raidyIds= raidyObject.getJSONArray("id");
                    JSONArray raidyNazwy= raidyObject.getJSONArray("nazwa");
                    //tv.append("\nArray zrobiony");


                    for (int i = 0; i < raidyNazwy.length(); i++)
                    {

                        raid_list.add(new RaidWpis(raidyIds.getString(i), raidyNazwy.getString(i)));
                        //listAdapter.add(raidyNazwy.getString(i) );
                    }
                    //tv.append("\n"+odpowiedz);
                }
                catch (ClientProtocolException e) 
                {
                    // TODO Auto-generated catch block
                    tv.append("\nBłąd protocol");
                } 
                catch (IOException e) 
                {
                    // TODO Auto-generated catch block
                    tv.append("\nBłąd IO 2");
                }

                // RaidWpis o1 = new RaidWpis("1","aaa");
                 // o1.setOrderName("SF services");
                 // o1.setOrderStatus("Pending");
                // RaidWpis o2 = new RaidWpis("2","bbb");
                  //o2.setOrderName("SF Advertisement");
                  //o2.setOrderStatus("Completed");
                 // raid_list.add(o1);
                 // raid_list.add(o2);
                  Thread.sleep(4000);
                //  Toast.makeText(getBaseContext(),  "dziala", Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    //Toast.makeText(getBaseContext(), "błąd", Toast.LENGTH_LONG).show();
                    Log.d("BACKGROUND_PROC", e.getMessage());
                }
                runOnUiThread(returnRes);
            }

我注意到它Log.d("BACKGROUND_PROC", e.getMessage());显示在 logcat

01-16 18:51:17.703: D/BACKGROUND_PROC(4175): Only the original thread that created a view hierarchy can touch its views.
4

1 回答 1

1

这个错误的主要原因是

在您的情况下,您正在尝试UI components从方法内部的后台线程访问 (View) Thread。你不能这样做。不允许这样做

编辑 :

您必须将更新 ui 的后台任务部分移动到主线程上。为此有一段简单的代码:

runOnUiThread(new Runnable() {
     public void run() {

//stuff that updates ui

    }
});

只需将其嵌套在后台运行的方法中,然后将实现任何更新的代码复制粘贴到块的中间。

于 2013-01-16T19:11:46.163 回答