我不明白僅創建視圖層次結構的原始線程的含義。請有人幫我解決這個問題。只有創建的原始線程
謝謝 ................................
This is my class
public class DashboardActivity extends Activity {
UserFunctions userFunctions;
Button btnLogout;
ListView mainListView;
SessionManager session;
private ArrayAdapter<String> listAdapter ;
public static String name;
public static String email;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
session = new SessionManager(getApplicationContext());
btnLogout = (Button) findViewById(R.id.button_logout);
mainListView = (ListView) findViewById(R.id.listView_groups_prof);
new Connection().execute();
}
private class Connection extends AsyncTask {
@Override
protected Object doInBackground(Object... arg0) {
connect();
return null;
}
protected void onPostExecute(Void result) {
//connect();
}
}
private String connect() {
if(session.isLoggedIn()){
HashMap<String, String> user = session.getUserDetails();
String idv = user.get(SessionManager.KEY_ID).toString();
String typev = user.get(SessionManager.KEY_TYPE).toString();
try {
URL url;
String myurl= "http://trouvermoi.com/android_login_api/src/controller/GroupeProfesseurController.php?tag=getgroupesprofesseur&idp="+idv;
System.out.println("URL : " + myurl);
url = new URL(myurl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
String data = InputStreamOperations.InputStreamToString(inputStream);
System.out.println("data : " + data);
JSONObject jsonObj = new JSONObject(data);
JSONArray empInfo = jsonObj.getJSONArray("emp_info");
final String[] values = new String[empInfo.length()];
ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 1);
for(int i = 0; i < empInfo.length(); i++){
JSONObject obj = empInfo.getJSONObject(i);
String groupe = obj.getString("groupe");
String professeur = obj.getString("professeur");
values[i]=groupe;
}
mainListView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , values));
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text
//Toast.makeText(getApplicationContext(), values[position].toString(),
// Toast.LENGTH_SHORT).show();
//When clicked evrywhere, open prologue.java
Intent intent = new Intent(DashboardActivity.this, Elevespargroupe.class);
intent.putExtra("id",values[position].toString());
startActivity(intent);
}
});
} catch (Exception e) {
e.printStackTrace();
}
btnLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
session.logoutUser();
Intent in = new Intent(DashboardActivity.this,LoginActivity.class);
startActivity(in);
}
});
}else{
Intent login = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(login);
}
return null;
}
}
這是我的logcat的
02-06 11:33:28.795: I/System.out(2358): data : {"emp_info":[{"id":"1","groupe":"1","professeur":"1"},{"id":"2","groupe":"2","professeur":"1"}]}
02-06 11:33:28.805: W/System.err(2358): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
02-06 11:33:28.815: W/System.err(2358): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
02-06 11:33:28.815: W/System.err(2358): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:854)
02-06 11:33:28.815: W/System.err(2358): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4077)
02-06 11:33:28.815: W/System.err(2358): at android.view.View.invalidate(View.java:10379)
02-06 11:33:28.815: W/System.err(2358): at android.view.View.invalidate(View.java:10334)
02-06 11:33:28.825: W/System.err(2358): at android.widget.AbsListView.resetList(AbsListView.java:1840)
02-06 11:33:28.825: W/System.err(2358): at android.widget.ListView.resetList(ListView.java:504)
02-06 11:33:28.825: W/System.err(2358): at android.widget.ListView.setAdapter(ListView.java:444)
02-06 11:33:28.825: W/System.err(2358): at com.androidhive.pushnotifications.DashboardActivity.connect(DashboardActivity.java:103)
02-06 11:33:28.835: W/System.err(2358): at com.androidhive.pushnotifications.DashboardActivity.access$0(DashboardActivity.java:71)
02-06 11:33:28.835: W/System.err(2358): at com.androidhive.pushnotifications.DashboardActivity$Connection.doInBackground(DashboardActivity.java:60)
02-06 11:33:28.835: W/System.err(2358): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-06 11:33:28.846: W/System.err(2358): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-06 11:33:28.846: W/System.err(2358): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-06 11:33:28.855: W/System.err(2358): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-06 11:33:28.855: W/System.err(2358): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-06 11:33:28.865: W/System.err(2358): at java.lang.Thread.run(Thread.java:856)
謝謝大家幫忙
問bazillion倍...不接觸從非UI線程在'UI元素...因此網絡操作(和解析)應該在'doInBackground'和適配器的創建和ListView設置onPostExecute' – Selvin 2015-02-06 11:49:06