0

我在我的应用程序中遇到了一些问题,有时它可以工作并显示我需要的所有数据,但有时它只是强制关闭我的应用程序,并在电话日志中显示出越界异常。顺便说一句,它在我的模拟器中工作得很好。不知道该怎么办了,所以在那里寻求帮助。这是我的电话记录。添加它只发生在 ICS 中,而不是 Gingerbread。

V/data content(11074): Data1
V/data id  (11074): 0
V/data name(11074): Data Name
V/data time(11074): Aug 25, 2012 10:00 PM
D/AndroidRuntime(11074): Shutting down VM
W/dalvikvm(11074): threadid=1: thread exiting with uncaught exception (group=0x40c531f8)
E/AndroidRuntime(11074): FATAL EXCEPTION: main
E/AndroidRuntime(11074): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
E/AndroidRuntime(11074):        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
E/AndroidRuntime(11074):        at java.util.ArrayList.get(ArrayList.java:304)
E/AndroidRuntime(11074):        at com.android.guestlist.DataActivity.showMyRsvp(RsvpActivity.java:167)
E/AndroidRuntime(11074):        at com.android.guestlist.DataActivity$AsyncGetRsvp.onPostExecute(DataActivity.java:92)
E/AndroidRuntime(11074):        at com.android.guestlist.DataActivity$AsyncGetRsvp.onPostExecute(DataActivity.java:1)
E/AndroidRuntime(11074):        at android.os.AsyncTask.finish(AsyncTask.java:602)
E/AndroidRuntime(11074):        at android.os.AsyncTask.access$600(AsyncTask.java:156)
E/AndroidRuntime(11074):        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
E/AndroidRuntime(11074):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(11074):        at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(11074):        at android.app.ActivityThread.main(ActivityThread.java:4517)
E/AndroidRuntime(11074):        at java.lang.reflect.Method.invokeNative(NativeMethod)
E/AndroidRuntime(11074):        at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(11074):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
E/AndroidRuntime(11074):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
E/AndroidRuntime(11074):        at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 2089):   Force finishing activity com.android.guestlist/.CustomTabActivity
E/android.os.Debug( 2089): !@Dumpstate > dumpstate -k -t -n -z -d -o /data/log/dumpstate_app_error

这是我的活动代码:

    public class RsvpActivity extends Activity {

    JSONArray jArray;
    String result = null;
    InputStream is = null;
    StringBuilder sb=null;

    String fb_id = "";
    String first_name = "";
    String last_name = "";
    String gender = "";

    public static List<String> eId3 = new ArrayList<String>();
    public static List<String> eBar3 = new ArrayList<String>();
    public static List<String> eName3 = new ArrayList<String>();
    public static List<String> eTime3 = new ArrayList<String>();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        fb_id =  getIntent().getStringExtra("fb_id");
        first_name =  getIntent().getStringExtra("first_name");
        last_name =  getIntent().getStringExtra("last_name");
        gender =  getIntent().getStringExtra("gender");

        setContentView(R.layout.rsvp);
    }

    protected void onStart(){
        super.onStart();
        new AsyncGetRsvp().execute(fb_id, "MY URL HERE");
    }

    protected void onResume(){
        super.onResume();
        new AsyncGetRsvp().execute(fb_id, "MY URL HERE");
    }

    public class AsyncGetRsvp extends AsyncTask<String, Void, List<String>>{
        private final ProgressDialog dialog = new ProgressDialog(RsvpActivity.this);

        @Override
        protected List<String> doInBackground(String... params) {
            getRsvp(params[0], params[1]);
            return null;
        }

        @Override
        protected void onPreExecute() {
             this.dialog.setMessage("Getting RSVPs");
             this.dialog.show();
        }

        @Override
        protected void onPostExecute(List<String> result) {
            showMyRsvp();
            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
             }
        }

    }


    public void showMyRsvp(){
        // Get the TableLayout
        final TableLayout tl = (TableLayout) findViewById(R.id.maintable2);
        tl.removeAllViewsInLayout();


        // Go through each item in the array
        for (int current = 0; current < eId3.size(); current++)
        {
            Log.v("bar content",eBar3.get(current));
            Log.v("bar id",eId3.get(current));
            Log.v("bar name",eName3.get(current));
            Log.v("bar time",eTime3.get(current));


            // Create a TableRow and give it an ID
            TableRow tr = new TableRow(this);
            tr.setId(100+current);
            tr.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));   

            // Create a TextView to house the name of the province
            final TextView clubName = new TextView(this);
            clubName.setId(200+current);
            clubName.setText(eBar3.get(current));
            clubName.setTextColor(Color.WHITE);
            clubName.setTextSize(12);
            clubName.setTypeface(null, Typeface.BOLD);
            clubName.setTag(eId3.get(current));
            clubName.setGravity(Gravity.CENTER_VERTICAL);
            clubName.setLayoutParams(new LayoutParams(
                    (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 65, getResources().getDisplayMetrics()),
                    LayoutParams.WRAP_CONTENT));
            tr.addView(clubName);

            //CREATE TEXT VIEW FOR EVENT NAME
            TextView eventName = new TextView(this);
            eventName.setId(current);
            eventName.setText(eName3.get(current));
            eventName.setGravity(Gravity.CENTER_VERTICAL);
            eventName.setTextSize(12);
            eventName.setTextColor(Color.WHITE);
            eventName.setLayoutParams(new LayoutParams(
                    (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 120, getResources().getDisplayMetrics()),
                    LayoutParams.WRAP_CONTENT));
            tr.addView(eventName);

          //CREATE TEXT VIEW FOR EVENT TIME
            TextView eventDate = new TextView(this);
            eventDate.setId(current);
            eventDate.setText(eTime3.get(current));
            eventDate.setGravity(Gravity.CENTER_VERTICAL);
            eventDate.setTextSize(12);
            eventDate.setTextColor(Color.WHITE);
            eventDate.setLayoutParams(new LayoutParams(
                    (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics()),
                    LayoutParams.WRAP_CONTENT));
            tr.addView(eventDate);

            // Add the TableRow to the TableLayout
            tl.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            final Intent i = new Intent(RsvpActivity.this, DetailsActivity.class);
            i.putExtra("bar_id",eId3.get(current));
            i.putExtra("fb_id", fb_id);
            i.putExtra("first_name",first_name);
            i.putExtra("last_name",last_name);
            i.putExtra("gender",gender);

            tr.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                     // eId3.get(current)
                    startActivity(i);
                }
            });

        }


    }



    public void getRsvp(String fb_id, String url){
        String result = "";

        eId3.clear();
        eBar3.clear();
        eName3.clear();
        eTime3.clear();

        //the year data to send
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

        nameValuePairs.add(new BasicNameValuePair("fb_id",fb_id));

           //http post
             try{
                     HttpClient httpclient = new DefaultHttpClient();
                     HttpPost httppost = new HttpPost(url);
                     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                     HttpResponse response = httpclient.execute(httppost);
                     HttpEntity entity = response.getEntity();
                     is = entity.getContent();
             }catch(Exception e){
                 Log.e("connection","error in http client");
             }

           //convert response to string
             try{
                     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                     sb = new StringBuilder();
                     String line = "0";

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


             try{
                     JSONArray jArray = new JSONArray(result);
                     JSONObject json_data=null;

                     for(int i=0;i<jArray.length();i++){
                             json_data = jArray.getJSONObject(i);
                             eId3.add(json_data.getString("event_id"));
                             eBar3.add(json_data.getString("bar_name"));
                             eName3.add(json_data.getString("event_name"));
                             eTime3.add(json_data.getString("date"));
                     }
                 }catch(JSONException e){
                     eId3.add("0");
                     eBar3.add("No Data");
                     eName3.add("No Data");
                     eTime3.add("No Data");
                         Log.e("log_tag", "Error parsing data "+e.toString());
                 }


    }

}
4

1 回答 1

1

问题是你开始两个AsyncTask做(我认为)同样的事情,一个在onStart回调中,一个在onResume回调中。因此,您最终将使用相同的列表完成两个任务。这很可能是您的一个列表大小为 的原因0,一个任务可能只是清除了列表,而第二个任务尝试访问该空列表。

于 2012-08-24T09:21:29.470 回答