0

在这个活动中,我想显示从 SQLite 到 Listview 的数据,但它仍然是运行时错误。你能检查一下这个 logcat 错误,告诉我发生了什么以及我该怎么做吗?

这是我的 logcat:我现在可以从 SQLite 获取数据

08-22 12:27:11.911: W/KeyCharacterMap(481): No keyboard for id 0
08-22 12:27:11.911: W/KeyCharacterMap(481): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
08-22 12:27:29.571: I/item_nickname(481): holly
08-22 12:27:29.571: I/item_fname(481): Yuma
08-22 12:27:29.571: I/item_lname(481): sama
08-22 12:27:29.571: I/item_nickname(481): mao
08-22 12:27:29.571: I/item_fname(481): ho
08-22 12:27:29.571: I/item_lname(481): koj
08-22 12:27:29.571: I/item_nickname(481): YYY
08-22 12:27:29.571: I/item_fname(481): GGG
08-22 12:27:29.571: I/item_lname(481): UUU
08-22 12:27:29.581: I/item_nickname(481): DDD
08-22 12:27:29.581: I/item_fname(481): HHH
08-22 12:27:29.581: I/item_lname(481): TTT
08-22 12:27:29.581: I/item_nickname(481): RICKY
08-22 12:27:29.581: I/item_fname(481): SAMA
08-22 12:27:29.581: I/item_lname(481): SUZA
08-22 12:27:29.581: I/item_nickname(481): LLLL
08-22 12:27:29.581: I/item_fname(481): bbb
08-22 12:27:29.591: I/item_lname(481): MMM
08-22 12:27:29.591: I/item_nickname(481): XXX
08-22 12:27:29.591: I/item_fname(481): VVV
08-22 12:27:29.591: I/item_lname(481): CCC
08-22 12:27:29.591: I/item_nickname(481): UUU
08-22 12:27:29.591: I/item_fname(481): LLL
08-22 12:27:29.591: I/item_lname(481): PPP
08-22 12:27:29.591: I/item_nickname(481): lang
08-22 12:27:29.591: I/item_fname(481): rong
08-22 12:27:29.591: I/item_lname(481): lang
08-22 12:27:29.591: I/item_nickname(481): white
08-22 12:27:29.591: I/item_fname(481): sim
08-22 12:27:29.601: I/item_lname(481): same
08-22 12:27:29.601: I/item_nickname(481): NONG
08-22 12:27:29.601: I/item_fname(481): Bome
08-22 12:27:29.601: I/item_lname(481): NING
08-22 12:27:29.601: I/item_nickname(481): JAN
08-22 12:27:29.601: I/item_fname(481): Ball
08-22 12:27:29.601: I/item_lname(481): JON
08-22 12:27:29.611: D/AndroidRuntime(481): Shutting down VM
08-22 12:27:29.611: W/dalvikvm(481): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-22 12:27:29.631: E/AndroidRuntime(481): FATAL EXCEPTION: main
08-22 12:27:29.631: E/AndroidRuntime(481): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqlite/com.example.sqlite.FriendsListActivity}: java.lang.IndexOutOfBoundsException: Invalid index 12, size is 12
08-22 12:27:29.631: E/AndroidRuntime(481):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-22 12:27:29.631: E/AndroidRuntime(481):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-22 12:27:29.631: E/AndroidRuntime(481):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-22 12:27:29.631: E/AndroidRuntime(481):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-22 12:27:29.631: E/AndroidRuntime(481):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 12:27:29.631: E/AndroidRuntime(481):  at android.os.Looper.loop(Looper.java:123)
08-22 12:27:29.631: E/AndroidRuntime(481):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-22 12:27:29.631: E/AndroidRuntime(481):  at java.lang.reflect.Method.invokeNative(Native Method)
08-22 12:27:29.631: E/AndroidRuntime(481):  at java.lang.reflect.Method.invoke(Method.java:507)
08-22 12:27:29.631: E/AndroidRuntime(481):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-22 12:27:29.631: E/AndroidRuntime(481):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-22 12:27:29.631: E/AndroidRuntime(481):  at dalvik.system.NativeStart.main(Native Method)
08-22 12:27:29.631: E/AndroidRuntime(481): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 12, size is 12
08-22 12:27:29.631: E/AndroidRuntime(481):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
08-22 12:27:29.631: E/AndroidRuntime(481):  at java.util.ArrayList.get(ArrayList.java:311)
08-22 12:27:29.631: E/AndroidRuntime(481):  at com.example.sqlite.FriendsListActivity.showAllList(FriendsListActivity.java:56)
08-22 12:27:29.631: E/AndroidRuntime(481):  at com.example.sqlite.FriendsListActivity.onCreate(FriendsListActivity.java:41)
08-22 12:27:29.631: E/AndroidRuntime(481):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-22 12:27:29.631: E/AndroidRuntime(481):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-22 12:27:29.631: E/AndroidRuntime(481):  ... 11 more

这是我的代码 FriendsListActivity

package com.example.sqlite;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.example.sqlite.db.FriendsDB;
import com.example.sqlite.entry.FriendEntry;

public class FriendsListActivity extends Activity {
private Context context;
private FriendsDB db;
private SimpleAdapter sAdap;
private ArrayList<FriendEntry> friends;
private TextView hellotext;
private ListView hellolistview;
ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.friendlist_layout); 
    context = this;
    db = new FriendsDB(context);
    friends = new ArrayList<FriendEntry>();

    showAllList();
}
public void showAllList(){
    //view matching
    hellotext = (TextView) findViewById(R.id.hellotext);
    hellolistview = (ListView) findViewById(R.id.hellolistview);
    //select data
    friends = db.selectAll();
        if(friends.size()==0){
            Toast.makeText(context,"You dont have any friend.",Toast.LENGTH_SHORT).show();
        }else{

            for (int i = 0;i<=friends.size();i++){
            // set value for friends    
                map = new HashMap<String, String>();
                map.put("item_nickname", friends.get(i).getNickname());
                map.put("item_fname", friends.get(i).getFname());
                map.put("item_lname", friends.get(i).getLname());

                Log.i("item_nickname", friends.get(i).getNickname());
                Log.i("item_fname", friends.get(i).getFname());
                Log.i("item_lname", friends.get(i).getLname());
            }

            //adapter   
            hellolistview.setAdapter(new adapter());
        }
    }


private class adapter extends BaseAdapter{
    private Holder holder;
    @Override

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

    @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;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        //create
        if( view == null){
            view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_layout,null);
            holder = new Holder();
            holder.nickname = (TextView) view.findViewById(R.id.item_nickname);
            holder.fname = (TextView)view.findViewById(R.id.item_fname);
            holder.lname = (TextView)view.findViewById(R.id.item_lname);
            view.setTag(holder);
        }else{
            holder = (Holder) view.getTag();
        }

        //assign 
        holder.nickname.setText(map.get("item_nickname"));
        holder.fname.setText(map.get("item_fname"));
        holder.lname.setText(map.get("item_lname"));


        return view;
    }

    private class Holder{

        public TextView nickname;
        public TextView fname;
        public TextView lname;
    }
}


}

谢谢

4

1 回答 1

1

更改 for 循环代码

for (int i = 1;i<=friends.size();i++){

for (int i = 0;i<friends.size();i++){

数组位置从 0 开始(不是从 1 开始)。在你的情况下,日志说列表的大小是 11。所以循环应该从 0 到 10(而不是从 1 到 11)读取数组的元素。


问题更新后:-

你又犯了一个错误

for (int i = 0;i<=friends.size();i++){你不应该<=只使用<所以替换 for 循环为

 for (int i = 0;i<friends.size();i++)
于 2013-08-22T05:20:31.903 回答