0

我正在尝试从数据库中获取一个值并将其添加到一个数组中以在列表视图中显示它。这是我的代码。

当我检查 arg1 时,我得到了值,我不明白我做错了什么。


高分法

list_score=(ListView)findViewById(R.id.score_list);//listview
        //get DB
        for(int i=0;i<3;i++){
            highscoreDB.execSQL("CREATE TABLE IF NOT EXISTS HIGHSCORE"+(i+3)+"(" +
                    "TIME long(9),MOVE integer(4),GRID integer(2));"
                   );
            Cursor gethighscorealter=highscoreDB.rawQuery("SELECT*FROM HIGHSCORE"+(i+3)+
                       " ORDER BY TIME asc, MOVE asc ;"
                       , null);
            if(gethighscorealter.getCount()>0){
                gethighscorealter.moveToFirst();
                timer= gethighscorealter.getLong(gethighscorealter.getColumnIndex("TIME")); 
                move = gethighscorealter.getInt(gethighscorealter.getColumnIndex("MOVE"));
                grid = gethighscorealter.getInt(gethighscorealter.getColumnIndex("GRID"));
                arg1[i]=timer;
                arg2[i]=move;
                arg3[i]=grid;
            }else{
                arg1[i]=1;
                arg2[i]=1;
                arg3[i]=i+3;
            }
        }
            list_score.setAdapter(new IntRangeAdapter(this,R.layout.score,arg1, arg2, arg3));


                highscoreview();

高分法

AlertDialog.Builder showhighscore =new AlertDialog.Builder(this);
            LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.high_score_list, null);//score
            showhighscore.setCancelable(false);
            showhighscore.setTitle(" ");
            showhighscore.setView(layout);
            showhighscore.setIcon(R.drawable.menu_icon);
            showhighscore.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.cancel();
                    onResume();
                }
            });
               showhighscore.show();

IntRangeAdapter 类

public class IntRangeAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private int mItemResource;
        private long[] time= new long[10];
        private int[] move=new int[10];
        private int[] grid=new int[10];

        public IntRangeAdapter(Context context, int itemLayout,long[] arg1,int[] arg2,int[] arg3) {
           mInflater = LayoutInflater.from(context);
            mItemResource = itemLayout;
            time=arg1;
            move=arg2;
            grid=arg3;
        }

        public int getCount() {
            return 4;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
               convertView = mInflater.inflate(mItemResource, parent, false);
            }

            TextView tv = (TextView) convertView.findViewById(R.id.textView1);
            TextView tv2 = (TextView) convertView.findViewById(R.id.textView2);
            TextView tv3 = (TextView) convertView.findViewById(R.id.textView3);

            tv.setText(""+time[position]);
            tv2.setText(move[position]);
            tv3.setText(grid[position]);
            return convertView;
        }
    }

我的原木猫

06-26 00:43:06.639: E/AndroidRuntime(1817): FATAL EXCEPTION: main
06-26 00:43:06.639: E/AndroidRuntime(1817): java.lang.NullPointerException
06-26 00:43:06.639: E/AndroidRuntime(1817):     at skripsi.slidame.PuzzleActivity.highscore(PuzzleActivity.java:283)
06-26 00:43:06.639: E/AndroidRuntime(1817):     at skripsi.slidame.PuzzleActivity.onOptionsItemSelected(PuzzleActivity.java:147)

PuzzleActivity.java:283list_score.setAdapter(new IntRangeAdapter(this,R.layout.score,arg1, arg2, arg3));

4

2 回答 2

1

很难知道为什么,没有看到整个程序。

我建议您尝试在调试模式下运行该应用程序。从空指针出现的地方向后工作。看看你在程序中的假设是否有意义。

我想,在没有更多信息的情况下,list_score 为空,因此尝试引用其中一种方法是行不通的。

于 2013-06-25T18:10:54.267 回答
0

既然我理解你在做什么更好一点,比使用 a 更简单的方法是Dialog将它变成 anActivity并且你可以给它 a Dialog Theme。只需添加到您的<activity>标签中manifest

android:theme="@android:style/Theme.Dialog"

但这里的根本问题是你试图访问score_list你的main.xml,但它存在于你high_score_list.xml用于你的Dialog. 您需要执行我的第一个建议或将您的代码移动listView到您的Dialog

AlertDialog.Builder showhighscore =new AlertDialog.Builder(this);
        LinearLayout layout = (LinearLayout)   
        getLayoutInflater().inflate(R.layout.high_score_list, null);//score
        ListView list_score = (ListView) layout.findViewById(R.id.score_list);  // add this line here and set adapter here
        list_score.setAdapter(new IntRangeAdapter(this,R.layout.score,arg1, arg2, arg3));
        showhighscore.setCancelable(false);
        showhighscore.setTitle(" ");
        showhighscore.setView(layout);
        showhighscore.setIcon(R.drawable.men 
于 2013-06-25T18:27:47.173 回答