我需要在列表視圖中顯示一些記錄,但是當我撥打服務時,它會讓我在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)
你確定該陣列中有足夠的物品嗎? – Royi 2012-02-28 11:18:25