开发者

The application has stopped unexpectedly! Arraylist Hashmaps and TabHost

I have a problem with my code and i don't seem to find it ! The error is...Application has stopped unexpectedly! I don't have a clue about what's going on. (i'm also a DevAndroid begginer) My first tab works ok, but when i switch to the second one with the ArrayList Hashmap, it crashes.

Here is my BonuriActivity code:

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

}

And here is my 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>

And my 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>

I gradly thank you for your time spent helping me. Please comme开发者_运维知识库nt if you see why is my program crashing, cause i'm already stressed out with this ('ve been on this for 2 days, it's frustrating).


Apparently you have no idea where you can find the LogCat...

If you develop with Eclipse, take a look here: http://developer.android.com/guide/developing/debug-tasks.html The error message you will find there is what we need to help you. So copy&paste it into your question.

If you want to learn how to debug, here is a small how to for it: http://www.droidnova.com/debugging-in-android-using-eclipse,541.html

update

Ok a screenshot from logcat isn't the best way to provide the error but it shows what Blundell suggested: You fetch from the internet in the UI thread. Read this: http://developer.android.com/guide/practices/design/responsiveness.html and the fetching of your internet stuff should be done with that: http://developer.android.com/reference/android/os/AsyncTask.html


You can also just create a new Thread and use a Handler:

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

}

This may not be guaranteed to work I'll check it later, but it show's the principle of threads and handlers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜