2011-02-17 82 views
0

我的代码有问题,我似乎没有找到它!错误是...应用程序意外停止!我不知道发生了什么。 (我也是一个DevAndroid的开始) 我的第一个选项卡工作正常,但是当我切换到第二个ArrayList Hashmap时,它崩溃。应用程序意外停止! Arraylist Hashmaps和TabHost

这里是我的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> 

我gradly感谢您的时间花了帮助我。 请评论,如果你看到我的程序崩溃的原因,因为我已经用这个压力(已经在这2天,这是令人沮丧)。

+2

请发布错误信息 – WarrenFaith 2011-02-17 19:56:17

+0

您正在请求您的代码在UI线程上进行互联网通话。所以你的行为变得缓慢而没有反应。尝试移动处理(互联网电话)到它自己的线程 – Blundell 2011-02-17 20:32:08

+0

@Blundell:他会得到一个“没有回应”的消息,而不是“意外停止”,或者我错了吗? – WarrenFaith 2011-02-17 20:33:33

回答

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

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); 
} 

} 

这可能无法保证工作,我稍后会检查它,但它显示了线程和处理程序的原理。