安卓视频查看应用崩溃

问题描述 投票:0回答:1

所以我写了这段代码,从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;
}

它甚至没有进入那个活动。是不是代码有什么问题。请大家帮帮我吧!日志猫信息。

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  
java android android-listview
1个回答
0
投票

我用这个代码工作,它的工作成功为我。

从SD卡中获取所有视频到列表视图。

 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.com1q5NP.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;
© www.soinside.com 2019 - 2024. All rights reserved.