我的代码有问题,我似乎找不到它!错误是......应用程序意外停止了!我不知道发生了什么。 (我也是DevAndroid begginer) 我的第一个选项卡工作正常,但是当我使用ArrayList Hashmap切换到第二个选项卡时,它会崩溃。
这是我的BonuriActivity代码:
public class BonuriActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_bonuri);
final String url="http://xxxzzz/throwdata.php?p=bonuri";
ListView lv = (ListView) findViewById(android.R.id.list);
SimpleAdapter adapter = new SimpleAdapter(BonuriActivity.this,
getBonuri(url), R.layout.list_bon,
new String[] {"Produs", "Cantitate", "UM", "IdVinzare"},
new int[] { R.id.Produs, R.id.Cantitate, R.id.UM, R.id.IdVinzare });
lv.setAdapter(adapter);
}
private ArrayList<HashMap<String,String>> getBonuri(String KEY_121) {
InputStream is = null;
String result = "";
HashMap<String,String> entitiesHashMap = new HashMap<String, String>();
ArrayList<HashMap<String, String>> returnString = new ArrayList<HashMap<String,String>>();
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
//Get an output to the screen
entitiesHashMap.put("Produs", json_data.getString("Produs"));
entitiesHashMap.put("Cantitate", json_data.getString("Cantitate"));
entitiesHashMap.put("UM", json_data.getString("UM"));
entitiesHashMap.put("IdVinzare", json_data.getString("IdVinzare"));
returnString.add(entitiesHashMap);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return returnString;
}
}
这是我的TabBonuri.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/tab2Layout">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
我的list_bon.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearView xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/UM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="UM"
android:textSize="16sp"
>
</TextView>
<TextView
android:id="@+id/Cantitate"
android:layout_width="wrap_content"
android:layout_height="21px"
android:padding="10dp"
android:text="Cantitate"
android:textSize="16sp"
>
</TextView>
<TextView
android:id="@+id/Produs"
android:layout_width="121px"
android:layout_height="22px"
android:padding="10dp"
android:text="Produs"
android:textSize="16sp"
>
</TextView>
<TextView
android:id="@+id/IdVinzare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:text="TextView"
/>
</LinearView>
我非常感谢您花时间帮助我。 请注意,如果你看到我的程序为什么会崩溃,因为我已经对此感到压力(已经有2天,这令人沮丧)。
答案 0 :(得分:1)
显然你不知道在哪里可以找到LogCat ......
如果您使用Eclipse进行开发,请查看此处:http://developer.android.com/guide/developing/debug-tasks.html 您将在那里找到的错误消息是我们需要帮助您的。因此,请将其复制并粘贴到您的问题中。
如果你想学习如何调试,这里有一个很小的方法:http://www.droidnova.com/debugging-in-android-using-eclipse,541.html
<强>更新强>
好吧,来自logcat的屏幕截图不是提供错误的最佳方式,但它显示了Blundell建议的内容:您在UI线程中从互联网上获取。阅读本文:http://developer.android.com/guide/practices/design/responsiveness.html并且应该使用以下内容获取您的互联网内容:http://developer.android.com/reference/android/os/AsyncTask.html
答案 1 :(得分:0)
您也可以创建一个新线程并使用处理程序:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class BonuriActivity extends ListActivity {
private static final int PASS = 0;
private static final int FAIL = 1;
private final String url = "http://xxxzzz/throwdata.php?p=bonuri";
private ArrayList<HashMap<String, String>> bonnieList = null;
private ProgressDialog mProgressDialog;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_bonuri);
fetchData();
}
private void fetchData() {
mProgressDialog = ProgressDialog.show(this, "Please Wait", "loading..", true);
new Thread() {
@Override
public void run() {
// Do the internet call in it's own thread
bonnieList = getBonuri(url);
// Call back to the UI thread with the result
if (bonnieList != null || !bonnieList.isEmpty()) {
doAfterInternetCall.sendEmptyMessage(PASS);
} else {
doAfterInternetCall.sendEmptyMessage(FAIL);
}
mProgressDialog.dismiss();
}
}.start();
}
private Handler doAfterInternetCall = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case PASS:
actUponRetreivedData();
break;
case FAIL:
// TODO do something else
break;
}
}
};
private ArrayList<HashMap<String, String>> getBonuri(String KEY_121) {
InputStream is = null;
String result = "";
HashMap<String, String> entitiesHashMap = new HashMap<String, String>();
ArrayList<HashMap<String, String>> returnString = new ArrayList<HashMap<String, String>>();
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// parse json data
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
// Get an output to the screen
entitiesHashMap.put("Produs", json_data.getString("Produs"));
entitiesHashMap.put("Cantitate", json_data.getString("Cantitate"));
entitiesHashMap.put("UM", json_data.getString("UM"));
entitiesHashMap.put("IdVinzare", json_data.getString("IdVinzare"));
returnString.add(entitiesHashMap);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return returnString;
}
protected void actUponRetreivedData() {
ListView lv = (ListView) findViewById(android.R.id.list);
SimpleAdapter adapter = new SimpleAdapter(BonuriActivity.this, bonnieList, R.layout.list_bon, new String[] { "Produs", "Cantitate", "UM", "IdVinzare" }, new int[] { R.id.Produs,
R.id.Cantitate, R.id.UM, R.id.IdVinzare });
lv.setAdapter(adapter);
}
}
这可能无法保证工作我稍后会检查它,但它显示了线程和处理程序的原理。