在面向ArrayIndexOutOfBoundsException的自定义列表视图中

时间:2012-04-30 11:20:07

标签: android arrays listview

我希望客户ListView带有2个文本框,在First我可以显示号码,在另一个我可以显示其未接来电号码,已接收或外出号码,日期我可以在ListView的Secod线显示日期和通话类型但是当我试图显示数字时,它会给出以下错误。

04-30 16:38:02.764: E/AndroidRuntime(7312): FATAL EXCEPTION: main
04-30 16:38:02.764: E/AndroidRuntime(7312): java.lang.ArrayIndexOutOfBoundsException
04-30 16:38:02.764: E/AndroidRuntime(7312): at com.custom.ListViewAdapter.getView  (ListViewAdapter.java:64)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.widget.AbsListView.obtainView(AbsListView.java:2203)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1204)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.widget.ListView.onMeasure(ListView.java:1115)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.view.View.measure(View.java:8339)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.view.View.measure(View.java:8339)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at   android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.view.View.measure(View.java:8339)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.view.View.measure(View.java:8339)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.view.View.measure(View.java:8339)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.os.Looper.loop(Looper.java:123)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at java.lang.reflect.Method.invokeNative(Native Method)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at java.lang.reflect.Method.invoke(Method.java:521)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:870)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
04-30 16:38:02.764: E/AndroidRuntime(7312):     at dalvik.system.NativeStart.main(Native Method)

在这里,我已将列表转换为 String [] ,但如果我写的话,我可以获得输出 lviewAdapter = new ListViewAdapter(this, timeArray, timeArray);,其中同时打印调用类型日期,但是当我写入时 lviewAdapter = new ListViewAdapter(this, timeArray, timeArray); 它提供 ArrayIndexoutOfBoundException

ListView2Activity.java

import java.sql.Date;

import java.util.ArrayList; import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ListView2Activity extends Activity implements OnItemClickListener
{
 /** Called when the activity is first created. */

ListView lview;
ListViewAdapter lviewAdapter;
List<String> dir;
List<String> num;
String[] numArray ;
String[] timeArray;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 
    dir = new ArrayList<String>();
    num = new ArrayList<String>();
    Cursor managedCursor = managedQuery( CallLog.Calls.CONTENT_URI,null, null,null, null);
    int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER ); 
    int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
    int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);

    Log.d("tag","call details");
    while ( managedCursor.moveToNext() ) 
    {
    String phNumber = managedCursor.getString( number );
    num.add(phNumber);

    String callType = managedCursor.getString( type );
    String callDate = managedCursor.getString( date );
    Date callDayTime = new Date(Long.valueOf(callDate));


    int dircode = Integer.parseInt( callType );
    switch( dircode )
    {
    case CallLog.Calls.OUTGOING_TYPE:
        dir.add("OUTGOING" + " , "+ callDayTime);

        break;
    case CallLog.Calls.INCOMING_TYPE:
        dir.add("INCOMING"+ " , "+ callDayTime);


        break;
    case CallLog.Calls.MISSED_TYPE:
        dir.add("MISSED"+  " , "+ callDayTime); 
        break;
    }// switch end
    Log.d("tag",phNumber +"," + dir );

    } // while end


     numArray = new String[num.size()];
    for (int i = 0; i < num.size(); i++) 
    {
        numArray[i] = num.get(i);  
    //  Log.i("tag", "in array :" + num[i]);
    } // for end

     timeArray = new String[dir.size()];
    for (int j = 0; j < dir.size(); j++) {
        timeArray[j] = dir.get(j);  
    } // for end

    managedCursor.close();

    lview = (ListView) findViewById(R.id.listView2);
    lviewAdapter = new ListViewAdapter(this, numArray, timeArray);

    System.out.println("adapter => "+lviewAdapter.getCount());

    lview.setAdapter(lviewAdapter);

    lview.setOnItemClickListener(this);
} // create end

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) 
{
    // TODO Auto-generated method stub
    Toast.makeText(this,"Title => "+numArray[position]+"=> n Description"+timeArray[position], Toast.LENGTH_SHORT).show();
}}

ListViewAdapter.java

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter
{
Activity context;
String title[];
String description[];

public ListViewAdapter(Activity context, String[] title, String[] description) {
    super();
    this.context = context;
    this.title = title;
    this.description = description;
}

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

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

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

private class ViewHolder {
    TextView txtViewTitle;
    TextView txtViewDescription;
}

public View getView(int position, View convertView, ViewGroup parent)
{
    // TODO Auto-generated method stub
    ViewHolder holder;
    LayoutInflater inflater =  context.getLayoutInflater();

    if (convertView == null)
    {
        convertView = inflater.inflate(R.layout.listitem_row, null);
        holder = new ViewHolder();
        holder.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
        holder.txtViewDescription = (TextView) convertView.findViewById(R.id.textView2);
        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtViewTitle.setText(title[position]);
    holder.txtViewDescription.setText(description[position]);

return convertView;
}
}

3 个答案:

答案 0 :(得分:3)

上面的代码运行良好我只需复制并粘贴它并运行此代码并获得以下结果。

enter image description here

答案 1 :(得分:0)

好吧这只是一个随机的猜测,但我认为你的一个数组是不同的大小。如果返回title.length&gt;这将导致您看到的错误; description.length。

推理 ok当调用getCount()时将返回存在多少视图。让我们假设title.length = 5.现在ListView将要生成五个视图,因此将调用getView for position = 1 ... 5。例如,如果description.length = 3,则在调用

时,getView()会得到一个异常,其中position = 4
holder.txtViewDescription.setText(description[position]);

答案 2 :(得分:0)

lviewAdapter = new ListViewAdapter(this, numArray, timeArray);
System.out.println("adapter => "+lviewAdapter.getCount());
lview.setAdapter(lviewAdapter);
lview.setOnItemClickListener(this);

替换上面的代码

if(timeArray.length > 0 )
{
   lviewAdapter = new ListViewAdapter(this, numArray, timeArray);
   System.out.println("adapter => "+lviewAdapter.getCount());
   lview.setAdapter(lviewAdapter);
   lview.setOnItemClickListener(this);
}