2012-02-28 99 views
0

我需要在列表視圖中顯示一些記錄,但是當我撥打服務時,它會讓我在adapter.so中發生錯誤,您能告訴我如何糾正它嗎?我在USERNAME中有Arrayindexoutofbound。我也有檢查的主要活動,但也有在all.this代碼沒有問題的previosly工作好。不過現在它提升問題....在撥打webservices時卡住

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) 
+0

你確定該陣列中有足夠的物品嗎? – Royi 2012-02-28 11:18:25

回答

1

你確定活動名稱和用戶名具有相同的大小?看來,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()不同。

+0

是的,我確定..... – 2012-02-28 11:20:38

+0

str_arr_ActivityName = new String [ActivityName.size()]; \t \t \t \t str_arr_ActivityName = ActivityName \t \t \t \t \t \t .toArray(str_arr_ActivityName); \t \t \t \t // ///////////// UserName ///////////////////////////// \t \t \t \t str_arr_UserName = new String [ActivityName.size()]; \t \t \t \t str_arr_UserName = UserName。指定者(str_arr_UserName); – 2012-02-28 11:21:00

+0

請參閱編輯2,尺寸可能不同。 – Sly 2012-02-28 11:27:31

0

它告訴你,你正試圖訪問一個數組超出其範圍。

具體來說,這裏:UserName[position]。如TeddyBearFr所述,此處的位置大於或等於UserName.length

你必須弄清楚,你有這個問題。

另外,你真的需要清理你的代碼位:

  • 注意你如何命名變量,這是不是Java友好。看看一些guidelines
  • 創建一個對象:Log將封裝了所有這些變量:scoreIdactivityNameuserName等等......然後,你的適配器將只讀的Log秒的陣列。
  • 當你點擊按鈕Ok時應該在後臺線程中完成,所以請嘗試使用AsyncTask