有人可以告诉我可能出现的问题吗?
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.book_list);
lst = (ListView) findViewById(R.id.listView1);
new Thread(new Runnable() {
public void run() {
data = fetchData();
runOnUiThread(new Runnable() {
@Override
public void run() {
ArrayList<String> users = parseJSON(data);
}
});
}
}).start();
final SimpleAdapter adapter =
new SimpleAdapter(this, mylistData, R.layout.rowlayout,
row , new int[] {R.id.label,R.id.label1});
lst.setAdapter(adapter);
05-24 11:08:32.218:E / AndroidRuntime(19121):致命异常:Thread-12 05-24 11:08:32.218:E / AndroidRuntime(19121):android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。 05-24 11:08:32.218:E / AndroidRuntime(19121):在android.view.ViewRoot.checkThread(ViewRoot.java:3041) 05-24 11:08:32.218:E / AndroidRuntime(19121):在android.view.ViewRoot.invalidateChild(ViewRoot.java:647) 05-24 11:08:32.218:E / AndroidRuntime(19121):在android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:673) 05-24 11:08:32.218:E / AndroidRuntime(19121):在android.view.ViewGroup.invalidateChild(ViewGroup.java:2511) 05-24 11:08:32.218:E / AndroidRuntime(19121):在android.view.View.invalidate(View.java:5255) 05-24 11:08:32.218:E / AndroidRuntime(19121):在android.widget.TextView.invalidateCursor(TextView.java:3774) 05-24 11:08:32.218:E / AndroidRuntime(19121):在android.widget.TextView.spanChange(TextView.java:6680) 05-24 11:08:32.218:E / AndroidRuntime(19121):在android.widget.TextView $ ChangeWatcher.onSpanAdded(TextView.java:6805) 05-24 11:08:32.218:E / AndroidRuntime(19121):在android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:906) 05-24 11:08:32.218:E / AndroidRuntime(19121):在android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:611) 05-24 11:08:32.218:E / AndroidRuntime(19121):在android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:514) 05-24 11:08:32.218:E / AndroidRuntime(19121):在android.text.Selection.setSelection(Selection.java:74) 05-24 11:08:32.218:E / AndroidRuntime(19121):在android.text.Selection.setSelection(Selection.java:85) 05-24 11:08:32.218:E / AndroidRuntime(19121):在android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:280) 05-24 11:08:32.218:E / AndroidRuntime(19121):在android.widget.TextView.setText(TextView.java:2798) 05-24 11:08:32.218:E / AndroidRuntime(19121):在android.widget.TextView.setText(TextView.java:2671) 05-24 11:08:32.218:E / AndroidRuntime(19121):在android.widget.EditText.setText(EditText.java:178) 05-24 11:08:32.218:E / AndroidRuntime(19121):在android.widget.TextView.setText(TextView.java:2646) 05-24 11:08:32.218:E / AndroidRuntime(19121):at com.example.book.ManageAuthor.parseJSON(ManageAuthor.java:198) 05-24 11:08:32.218:E / AndroidRuntime(19121):at com.example.book.ManageAuthor $ 1.run(ManageAuthor.java:77) 05-24 11:08:32.218:E / AndroidRuntime(19121):at java.lang.Thread.run(Thread.java:1019)
答案 0 :(得分:0)
尝试使用http://developer.android.com/reference/android/view/View.html#post(java.lang.Runnable)。
例如:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.book_list);
lst = (ListView) findViewById(R.id.listView1);
new Thread(new Runnable() {
public void run() {
data = fetchData();
lst.post(new Runnable() {
@Override
public void run() {
ArrayList<String> users = parseJSON(data);
SimpleAdapter adapter = new SimpleAdapter(this, mylistData, R.layout.rowlayout, row , new int[] {R.id.label,R.id.label1});
lst.setAdapter(adapter);
}
});
}
}).start();
通过这种方式,您可以异步解析JSON数据并以线程安全的方式修改listview的数据。