嗨,我的 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.