在调用webservices时被困住了

时间:2012-02-28 11:12:26

标签: android

我需要在listview中显示一些记录,但是当我调用服务时,它会在adapter中出错。那么你能告诉我如何更正它吗?我在USERNAME中得到了Arrayindexoutofbound。我也检查了主要活动,但没有任何问题。这个代码普遍有效。但是现在它提出了问题....

package com.RecordingAppDotnet;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.Sharedpreferance.GMailSender;

public class LogListAdapter extends BaseAdapter
{

    public String ScoreID[];
    public String Activityname[];
    public String UserName[];
    public String PlayedDate[];
    public String UserComment[];
    public String Score[];
    public int int_spnr_choose_item_first_or_sec;
    public Activity context;
    public LayoutInflater inflater;

    public LogListAdapter(Activity context,String[] ScoreID,String Activityname[],String UserName[],String PlayedDate[],String UserComment[],String Score[],int int_spnr_choose_item_first_or_sec) {
        super();

        this.context = context;
        this.ScoreID = ScoreID;
        this.Activityname = Activityname;
        this.UserName = UserName;
        this.PlayedDate = PlayedDate;
        this.UserComment= UserComment;
        this.Score = Score;
        this.int_spnr_choose_item_first_or_sec = int_spnr_choose_item_first_or_sec;
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return Activityname.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder
    {
        TextView log_Activity,log_username,log_playdate,log_score;
        Button edit_comment;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        ViewHolder holder;
        if(convertView==null)
        {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.log_activity_row, null);
            holder.log_Activity = (TextView) convertView.findViewById(R.id.log_Activity_name);
            holder.log_username = (TextView) convertView.findViewById(R.id.log_Username);
            holder.log_playdate = (TextView) convertView.findViewById(R.id.log_PlayDate);
            holder.log_score = (TextView) convertView.findViewById(R.id.log_Score);
            holder.edit_comment = (Button)convertView.findViewById(R.id.log_comment_btn);




            convertView.setTag(holder);
        }
        else
            holder=(ViewHolder)convertView.getTag();
        holder.log_Activity.setText(Activityname[position]);
        holder.log_username.setText(UserName[position]);
        holder.log_playdate.setText(PlayedDate[position]);
        holder.log_score.setText(Score[position]);
        //final Intent editcommentIntent = new Intent(context,LogList.class);
        holder.edit_comment.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {



                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.comment);
                dialog.setTitle("Put your Comment");
                dialog.setCancelable(true);

                final EditText cmment_comment_edtx;
                Button cmt_btn_ok,cmt_cacel;
                cmment_comment_edtx =(EditText)dialog.findViewById(R.id.cmment_comment_edtx);
                //UserComment[position].replaceAll("\\%0A", "");
                UserComment[position] = URLDecoder.decode(UserComment[position].toString());
                cmment_comment_edtx.setText(UserComment[position].toString());
                cmt_btn_ok = (Button)dialog.findViewById(R.id.cmt_btn_ok);
                cmt_cacel = (Button)dialog.findViewById(R.id.cmt_cacel);
                cmment_comment_edtx.setOnEditorActionListener(new OnEditorActionListener(){ 

                    @Override
                    public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                         return false;
                    }

                 });

                cmt_btn_ok.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                            try
                            {
                            String Comment = cmment_comment_edtx.getText().toString().trim();

                            String query = URLEncoder.encode(Comment, "utf-8");
                                try
                                {                                         //Webservices/Service.asmx/UpdateLogComment?ScoreId=string&MachineName=string&UserComment=string
                                    String sourceUrl1 = "URL/Service.asmx/UpdateLogComment?ScoreId="+ScoreID[position]+"&MachineName="+Machinelist.str_Machinename+"&UserComment="+query+"";
                                    URL sourceUrl = new URL(sourceUrl1);
                                    SAXParserFactory spf = SAXParserFactory.newInstance();
                                    SAXParser sp = spf.newSAXParser();
                                    XMLReader xr = sp.getXMLReader();
                                    commentHandler dataHandler = new commentHandler();
                                    xr.setContentHandler(dataHandler);
                                    xr.parse(new InputSource(sourceUrl.openStream()));
                                    commentDataset dataset = dataHandler.getParsedcomment_DataSet();

                                    String flag_sucess_send_commnt = dataset.getboolean1();
                                    if(flag_sucess_send_commnt.equalsIgnoreCase("true"))
                                    { 
                                        Toast.makeText(context, "Comment added Successfully.",Toast.LENGTH_LONG).show();
                                        dialog.cancel();
                                        LogList.lview.setVisibility(View.INVISIBLE);

                                    }
                                    else
                                    {
                                        Toast.makeText(context, "Comment not Update,Please Try Again", Toast.LENGTH_LONG).show();
                                    }
                                }catch (Exception e) {
                                    e.printStackTrace();
                                }

                    }catch (Exception e) {
                        Writer writer = new StringWriter();
                        PrintWriter printWriter = new PrintWriter(writer);
                        e.printStackTrace(printWriter);
                        String s = writer.toString();

                        } catch (Exception ex) {   

                        } 
                    }}
                });

                cmt_cacel.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        dialog.cancel();
                    }
                });
                dialog.show();


            }
        });


        return convertView;
    } 

}

错误:

02-28 16:25:54.082: ERROR/AndroidRuntime(3374): FATAL EXCEPTION: main
02-28 16:25:54.082: ERROR/AndroidRuntime(3374): java.lang.ArrayIndexOutOfBoundsException
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at com.RecordingAppDotnet.LogListAdapter.getView(LogListAdapter.java:107)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.AbsListView.obtainView(AbsListView.java:1428)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1265)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.ListView.onMeasure(ListView.java:1128)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.View.measure(View.java:8553)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.View.measure(View.java:8553)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.View.measure(View.java:8553)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.View.measure(View.java:8553)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.View.measure(View.java:8553)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.View.measure(View.java:8553)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.ViewRoot.performTraversals(ViewRoot.java:915)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1991)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.os.Looper.loop(Looper.java:150)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.app.ActivityThread.main(ActivityThread.java:4385)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at java.lang.reflect.Method.invokeNative(Native Method)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at java.lang.reflect.Method.invoke(Method.java:507)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

您确定Activityname和Username的大小相同吗?似乎Activityname.length> Username.length

编辑:在构造函数中添加它以检查数组大小

    Log.d("TEST", "Activityname size = " + Activityname.length );
    Log.d("TEST", "UserName size     = " + UserName.length );
    Log.d("TEST", "PlayedDate size   = " + PlayedDate.length );
    Log.d("TEST", "UserComment size  = " + UserComment.length );
    Log.d("TEST", "Score size        = " + Score.length );

EDIT2:不要这么肯定!

根据Android documentation,如果集合的大小大于参数数组,则collection.toArray(Object [])方法将创建一个新数组。因此,str_arr_ActivityName和str_arr_UserName的大小可能与ActivityName.size()不同。

答案 1 :(得分:0)

它告诉您正在尝试访问其范围之外的数组。

具体来说,这里:UserName[position]。正如TeddyBearFr所述,此处的位置大于或等于UserName.length

你必须弄明白,你遇到了这个问题。

此外,您确实需要稍微清理一下代码:

  • 注意你如何命名你的变量,这不是Java友好的。看看一些guidelines
  • 创建一个对象:Log,它将封装所有这些变量:scoreIdactivityNameuserName等等......然后,您的适配器将只读取一个数组Log s。
  • 点击按钮Ok时您正在做的事情应该在后台线程中完成,因此请尝试使用AsyncTask