我目前正在编写一个程序,将服务器中的数据作为xml文件存储,并将其存储在一个简单的数据库中,并在google地图上显示位置
我为Android 2.2开发了应用程序,我在模拟器中使用android ICS进行了测试。它按照我的预期工作,但当我在运行Android 4.04的Android设备上尝试时,应用程序卡住了
如果你知道为什么会这样,请帮助我 谢谢
答案 0 :(得分:0)
@arcastro,@ Stephan Celis,@ Egor和@Mohan非常感谢你的帮助。你的所有想法对我帮助很大。 我添加一个单独的线程连接到网络并下载数据@Egor你的问题让我意识到我做错了
谢谢大家
对于遇到同样问题的开发人员,我听到的是我是如何做到的
放在班级“updateHandler”代码下面
private Handler updateHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
if (msg.what == -999)
{
Log.d("recieve data","from http://jayanga.esplprojects.com/xx/data.xml to update");
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
String xml = XML.getXML("http://jayanga.esplprojects.com/xx/data.xml");
Document doc = XML.XMLfromString(xml);
int numResults = XML.numResults(doc);
if((numResults <= 0)){
Toast.makeText(getApplicationContext(), "SORRY No data were found", Toast.LENGTH_LONG).show();
finish();
}//if((numResults <= 0))
NodeList nodes = doc.getElementsByTagName("result");
for (int i = 0; i < nodes.getLength(); i++) {
Element e = (Element)nodes.item(i);
// String s = "...."+XML.getValue(e, "name");
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addContact(new Details(XML.getValue(e, "name"),Double.parseDouble( XML.getValue(e, "latitude"))
, Double.parseDouble(XML.getValue(e, "longitude")),XML.getValue(e, "contact") ));
}// for (int i = 0; i < nodes.getLength(); i++)
db.close();
Toast.makeText(getApplicationContext(), "Database Successfully Updated", Toast.LENGTH_LONG).show();
displayLocations();
}
}
};
然后从您想要运行线程的地方调用updateHandler
int get_update_data = -999;
Message theMessage = updateHandler .obtainMessage(get_update_data);
updateHandler.sendMessage(theMessage);//Sends the message to the UI handler.
答案 1 :(得分:0)
这个问题有两个解决方案,但我建议你使用第一个解决方案,因为第二个解决方案只是隐藏问题。
1)不要在Main UIThread,Use Async Task中编写网络调用。
2)在setContentView(R.layout.activity_main)之后将下面的代码写入MainActivity文件;
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
将import语句放到你的java文件中。
import android.os.StrictMode;
答案 2 :(得分:-1)
if (android.os.Build.VERSION.SDK_INT > 8) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
if you can add the above code then you can check in 4.0 version mobile, it will work happy.