简体   繁体   中英

Android Video View App crashing

So I wrote this code which gets the video from the sd card and displays it in a listview, but when I start it, the app automatically crashes. Here's the code:

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

It does not even go into that activity. Is there anything wrong with the code. Please help me out.Log Cat info:

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  

I've never seen an ArrayAdapter used like this. It should return View's where appropriate. Android is telling you it was looking for a TextView but found a LinearLayout. Nothing really to do with a VideoView.

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

i work with this code and it work succes for me

for get all videos from sdcard into list view:

 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();

now for open each list item video in 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);
        }
    });

and in videoview class to play video use this:

 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();
            }
        });

dont forgot use:

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

and this (in videoview class):

Intent i = getIntent();

see image also: http://i.stack.imgur.com/1q5NP.jpg

Update:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM