当我开始系统发送my:
时,我有一个问题是将List从Simple Adpater发送到布局public class Lista extends ListActivity
{
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
private static final String PREFERENCES_NAME = "myPreferences";
private static final String PREFERENCES_TEXT_FIELDK = "key";
TextView result;
String text;
// XML node keys
static final String KEY_ITEM = "quote"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_COST = "date";
static final String KEY_DESC = "place";
static final String KEY_POLEC = "action";
private SharedPreferences preferences;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.lista);
preferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);
result = (TextView)findViewById(R.id.info);
try {checksession();}catch(JSONException e){/* TODO Auto-generated catch block */ e.printStackTrace();}
}
public void checksession() throws JSONException
{
// Create a new HttpClient and Post Header
Thread thread = new Thread()
{
@Override
public void run()
{
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost(xxx); // make sure the url is correct.
......................
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable()
{
public void run()
{
// p.dismiss();
if (response != null && response.length()==6)
{
// result.setText("In System");
takeXML(); // takeXML XML
}else{ result.setText("No login:"+response);}
}
});
}catch(Exception e){
//runOnUiThread(new Runnable() {public void run() {p.dismiss();}});
result.setText("Error system");
System.out.println("Exception : " + e.getMessage());
}
}
};
thread.start();
}
在这个位置代码没问题。现在是代码函数takeXML:
private void takeXML()
{
new Thread(new Runnable() {
public void run()
{
try{
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
String adresxml="http://xxx.xml";
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(adresxml); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
for (int i = 0; i < nl.getLength(); i++)
{
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_COST, parser.getValue(e, KEY_COST));
// adding HashList to ArrayList
// excel[i] = new ExcelFileClass()
menuItems.add(map);
}
Log.v("List","Array:"+menuItems);
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(getBaseContext(), menuItems,R.layout.list_item,new String[] { KEY_NAME, KEY_COST, KEY_ID }, new int[] {R.id.name,R.id.cost, R.id.elements });
现在结束是问题,在这个地方系统发送给我: “android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。” 我试试,我正在寻找如何解决这个问题。但是,不知道这个。
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
......
}
});
}catch(Exception e){
//runOnUiThread(new Runnable() {public void run() {p.dismiss();}});
result.setText("Error");
System.out.println("Exception : " + e.getMessage());
}
}
}).start();
}
答案 0 :(得分:0)
您需要调用代码部分来修改活动主线程内的UI。
要执行此操作,请使用runOnUiThread
来呼叫setListAdapter(adapter)
:
runOnUiThread(new Runnable() {
@Override
public void run() {
setListAdapter(adapter)
}
});
对result.setText("Error");
执行相同操作。
修改强>
private void takeXML()
{
new Thread(new Runnable() {
public void run()
{
try{
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
String adresxml="http://xxx.xml";
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(adresxml); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
for (int i = 0; i < nl.getLength(); i++)
{
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_COST, parser.getValue(e, KEY_COST));
// adding HashList to ArrayList
// excel[i] = new ExcelFileClass()
menuItems.add(map);
}
Log.v("List","Array:"+menuItems);
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(getBaseContext(), menuItems,R.layout.list_item,new String[] { KEY_NAME, KEY_COST, KEY_ID }, new int[] {R.id.name,R.id.cost, R.id.elements });
runOnUiThread(new Runnable() {
@Override
public void run() {
setListAdapter(adapter)
}
});
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
......
}
});
}catch(Exception e){
//runOnUiThread(new Runnable() {public void run() {p.dismiss();}});
runOnUiThread(new Runnable() {
@Override
public void run() {
result.setText("Error");
}
});
System.out.println("Exception : " + e.getMessage());
}
}
}).start();
}
希望有所帮助:)