单击按钮时的Inflater Layout

时间:2017-09-01 13:07:16

标签: android sqlite

我正在使用此代码播放视频:

void videoIntilize(View v, final Context ctx, final Activity act)
{
    SQLiteDatabase db;
    db = act.openOrCreateDatabase("HadsKalme", Context.MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS videos (word VARCHAR,path TEXT,regdate VARCHAR);");

    final Cursor c = db.rawQuery("SELECT * FROM videos ORDER BY regdate DESC", null);
    if (c.getCount() == 0) {
        //Nothing TODO
    } else {
        int i = 0;
        LayoutInflater vi = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Typeface face = Typeface.createFromAsset(act.getAssets(),
                "fonts/IRANSANS.TTF");
        while (c.moveToNext()) {
            View ve = vi.inflate(R.layout.videotemplate, null, false);
            // fill in any details dynamically here
            TextView txt1 = (TextView) ve.findViewById(R.id.title);
            txt1.setText(" ویدئوی کلمه "+c.getString(0));
            TextView txt2 = (TextView) ve.findViewById(R.id.date);
            txt2.setText(" در تاریخ  "+c.getString(2));
            txt2.setTypeface(face);
            txt1.setTypeface(face);
            ImageView img = (ImageView) ve.findViewById(R.id.img);
            Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(c.getString(1),
                    MediaStore.Images.Thumbnails.MINI_KIND);
            img.setImageBitmap(thumbnail);
            ImageView player = (ImageView) ve.findViewById(R.id.playvideo);
            player.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(c.getString(1)));
                    intent.setDataAndType(Uri.parse(c.getString(1)), "video/mp4");
                    act.startActivity(intent);
                }
            });
            // insert into main view
            ViewGroup insertPoint = (ViewGroup) v.findViewById(R.id.lnrelans);
            ve.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            insertPoint.addView(ve);
            i++;
        }
    }
}

在此代码中我使用的是Inflater Layout,当我点击播放视频时会出现此错误:

E / AndroidRuntime:致命异常:主要                                                                    处理:ir.hiup.hadskalme,PID:22609                                                                    android.database.CursorIndexOutOfBoundsException:请求索引9,大小为9

怎么能解决这个问题? 感谢您的时间和帮助;

1 个答案:

答案 0 :(得分:0)

您不应直接使用onClick方法中的游标c。 在以下代码中:

替换此代码

 player.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(c.getString(1)));
                intent.setDataAndType(Uri.parse(c.getString(1)), "video/mp4");
                act.startActivity(intent);
            }
        });

 final String cursorUri = c.getString(1);
 player.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(cursorUri ));
                    intent.setDataAndType(Uri.parse(cursorUri), "video/mp4");
                    act.startActivity(intent);
                }
            });

当您直接使用光标时,单击按钮时,光标指向afterLast之后的位置,这显然不存在,因此它会给出异常。 希望这个答案很有帮助。