Android视频应用程序崩溃

时间:2015-01-18 05:13:30

标签: java android android-listview

所以我编写了这段代码,它从SD卡中获取视频并将其显示在列表视图中,但是当我启动它时,应用程序会自动崩溃。 这是代码:

private String[] videolist;
private String[] address;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video_player);
    getSupportActionBar().hide();

    ListView lv = (ListView)findViewById(R.id.lv);
    videolist = getVideolist();
    address = getAddresslist();

    ArrayAdapter<String> mAdapter = new ArrayAdapter<>(this,android.R.layout.activity_list_item,videolist);
    lv.setAdapter(mAdapter);
    lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
            String path = videolist[position];

            Intent i = new Intent(VideoPlayer.this,VideoV.class);
            i.putExtra("path2", path);
            startActivity(i);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent){

        }
    });
}


private String[] getAddresslist(){
    final Cursor mCursor = getContentResolver().query(
        MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
        new String[] {MediaStore.Video.Media.DISPLAY_NAME,MediaStore.Video.Media.DATA},
        null, null,
        "LOWER(" + MediaStore.Video.Media.TITLE + ") ASC"
    );

    int count = mCursor.getCount();

    String[] mVideoPath = new String[count];
    int i = 0;
    if (mCursor.moveToFirst()){
        do{
            mVideoPath[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
            i++;
        }while(mCursor.moveToNext());
    }

    mCursor.close();

    return mVideoPath;
}


private String[] getVideolist(){
    final Cursor mCursor = getContentResolver().query(
        MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
        new String[] {MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DATA},
        null, null,
        "LOWER(" + MediaStore.Video.Media.TITLE + ") ASC"
    );

    int count = mCursor.getCount();

    String[] mVideoList = new String[count];
    int i = 0;
    if(mCursor.moveToFirst()){
        do{
            mVideoList[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME));

            i++;
        }while(mCursor.moveToNext());
    }

    mCursor.close();

    return mVideoList;
}

它甚至没有进入那项活动。代码有什么问题吗?请帮帮我。登录Cat信息:

01-18 10:47:02.243  10109-10109/app.my.com.bonesapp W/dalvikvm﹕                                                                   threadid=1: thread exiting with uncaught exception (group=0x400207d8)01-18 10:47:02.353  10109-10109/app.my.com.bonesapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
        at android.widget.AbsListView.obtainView(AbsListView.java:1448)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1273)
        at android.widget.ListView.onMeasure(ListView.java:1184)
        at android.view.View.measure(View.java:8171)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:578)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:362)
        at android.view.View.measure(View.java:8171)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
        at android.view.View.measure(View.java:8171)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
        at android.view.View.measure(View.java:8171)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
        at android.view.View.measure(View.java:8171)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
        at android.view.View.measure(View.java:8171)
        at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
        at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:4633)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:521)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassCastException: android.widget.LinearLayout
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:34 at        android.widget.ArrayAdapter.getView(ArrayAdapter.java:323 at   

     android.widget.AbsListView.obtainView(AbsListView.java:1448)at  

2 个答案:

答案 0 :(得分:0)

我从来没有见过像这样使用的ArrayAdapter。它应该在适当的时候返回View。 Android告诉你它正在寻找TextView但发现了一个LinearLayout。与VideoView没什么关系。

https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

答案 1 :(得分:0)

我使用此代码,它对我来说是成功的

将sdcard中的所有视频转换为列表视图:

 c = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
    while (c.moveToNext()) {
        int vol = c.getColumnIndex(MediaStore.Video.Media.DATA);
        int name = c.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME);
        int size = c.getColumnIndex(MediaStore.Video.Media.RESOLUTION);
        int duration=c.getColumnIndex(MediaStore.Video.Media.DURATION);
        int hours = duration / 3600;
        int minutes = (duration / 60) - (hours * 60);
        int seconds = duration - (hours * 3600) - (minutes * 60) ;
        String formatted = String.format("%d:%02d:%02d", hours, minutes, seconds);
        String getdata = c.getString(vol);
        String getname = c.getString(name);
        String getsize = c.getString(size);
        as.add(getsize);
        al.add(getdata);
        an.add(getname);
        at.add(formatted);
        aa = new MyVideoAdapter(this, android.R.layout.simple_list_item_1,al);
        list.setAdapter(aa);
    }
    c.close();

现在打开videoview中的每个列表项视频:

 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String getpath = al.get(i);
            String getname = an.get(i);
            String getsize = as.get(i);
            Intent start = new Intent(MainActivity.this,MainActivity2.class);
            start.putExtra("data",getpath);
            start.putExtra("name",getname);
            start.putExtra("size" ,getsize);
            start.setAction("dirct");
            startActivityForResult(start, 12);
        }
    });

并在videoview类中播放视频使用:

 String path = i.getExtras().getString("data");
        vv = (VideoView) findViewById(R.id.videoView);
        vv.setVideoPath(path);
        vv.setMediaController(new MediaController(this));
        vv.start();
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        android.widget.RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) vv.getLayoutParams();
        params.width = metrics.widthPixels;
        params.height = metrics.heightPixels;
        params.leftMargin = 0;
        vv.setLayoutParams(params);

        vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                setResult(RESULT_OK);
                finish();
            }
        });

别忘了使用:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

和(在videoview类中):

Intent i = getIntent();

另见图片:http://i.stack.imgur.com/1q5NP.jpg

更新

private ArrayList<String> al = new ArrayList<String>();
private ArrayList<String> an = new ArrayList<String>();
private ArrayList<String> at = new ArrayList<String>();
private ArrayList<String> as = new ArrayList<String>();
private MyVideoAdapter aa;