2016-01-30 81 views
-1

我嘗試製作SD卡中所有電影的列表。一切都很好,但應用程序停止。我在日誌中找不到錯誤。 這個想法是有一個本地電影列表,但沒有使其工作。 請幫忙! 非常感謝你列表視頻android,代碼錯誤

代碼:

public class main extends Activity { 
//set constants for MediaStore to query, and show videos 
private final static Uri MEDIA_EXTERNAL_CONTENT_URI = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; 
private final static String _ID = MediaStore.Video.Media._ID; 
private final static String MEDIA_DATA = MediaStore.Video.Media.DATA; 
//flag for which one is used for images selection 
private GridView _gallery; 
private Cursor _cursor; 
private int _columnIndex; 
private int[] _videosId; 
private Uri _contentUri; 
String filename; 
int flag = 0; 



protected Context _context; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    _context = getApplicationContext(); 
    setContentView(R.layout.activity_main); 
    //set GridView for gallery 
    _gallery = (GridView) findViewById(R.id.videoGrdVw); 
    //set default as external/sdcard uri 
    _contentUri = MEDIA_EXTERNAL_CONTENT_URI; 

    initVideosId(); 

    //set gallery adapter 
    setGalleryAdapter(); 


} 
private void setGalleryAdapter() { 
    _gallery.setAdapter(new VideoGalleryAdapter(_context)); 
    _gallery.setOnItemClickListener(_itemClickLis); 
    flag = 1; 
} 
private AdapterView.OnItemClickListener _itemClickLis = new OnItemClickListener() 
{ 
    @SuppressWarnings({ "deprecation", "unused", "rawtypes" }) 
    public void onItemClick(AdapterView parent, View v, int position, long id) 
    { 
     // Now we want to actually get the data location of the file 
     String [] proj={MEDIA_DATA}; 
     // We request our cursor again 
     _cursor = managedQuery(_contentUri, 
       proj, // Which columns to return 
       MEDIA_DATA + " like ? ",  // WHERE clause; which rows to return (all rows) 
       new String[] {"%Movies%"},  // WHERE clause selection arguments (none) 
       null); // Order-by clause (ascending by name) 
     // We want to get the column index for the data uri 
     int count = _cursor.getCount(); 
     // 
     _cursor.moveToFirst(); 
     // 
     _columnIndex = _cursor.getColumnIndex(MEDIA_DATA); 
     // Lets move to the selected item in the cursor 
     _cursor.moveToPosition(position); 
     // And here we get the filename 
     filename = _cursor.getString(_columnIndex); 
     //*********** You can do anything when you know the file path :-) 
     showToast(filename); 

     Intent i = new Intent(main.this, Player.class); 
     i.putExtra("videoPath", filename); 
     startActivity(i); 


     // 
    } 
}; 
@SuppressWarnings("deprecation") 
private void initVideosId() { 
    try 
    { 
     //Here we set up a string array of the thumbnail ID column we want to get back 
     String [] proj={_ID}; 
     // Now we create the cursor pointing to the external thumbnail store 
     _cursor = managedQuery(_contentUri, 
       proj, // Which columns to return 
       MEDIA_DATA + " like ? ",  // WHERE clause; which rows to return (all rows) 
       new String[] {"%Movies%"},  // WHERE clause selection arguments (none) 
       null); // Order-by clause (ascending by name) 
     int count= _cursor.getCount(); 
     // We now get the column index of the thumbnail id 
     _columnIndex = _cursor.getColumnIndex(_ID); 
     //initialize 
     _videosId = new int[count]; 
     //move position to first element 
     _cursor.moveToFirst(); 
     for(int i=0;i<count;i++) 
     { 
      int id = _cursor.getInt(_columnIndex); 
      // 
      _videosId[i]= id; 
      // 
      _cursor.moveToNext(); 
      // 
     } 
    }catch(Exception ex) 
    { 
     showToast(ex.getMessage().toString()); 
    } 

} 
protected void showToast(String msg) 
{ 
    Toast.makeText(_context, msg, Toast.LENGTH_LONG).show(); 
} 

// 
private class VideoGalleryAdapter extends BaseAdapter 
{ 
    public VideoGalleryAdapter(Context c) 
    { 
     _context = c; 
    } 
    public int getCount() 
    { 
     return _videosId.length; 
    } 
    public Object getItem(int position) 
    { 
     return position; 
    } 
    public long getItemId(int position) 
    { 
     return position; 
    } 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     ImageView imgVw= new ImageView(_context);; 
     try 
     { 
      if(convertView!=null) 
      { 
       imgVw= (ImageView) convertView; 
      } 
      imgVw.setImageBitmap(getImage(_videosId[position])); 
      imgVw.setLayoutParams(new GridView.LayoutParams(200, 200)); 
      imgVw.setPadding(8, 8, 8, 8); 
     } 
     catch(Exception ex) 
     { 
      System.out.println("main:getView()-135: ex " + ex.getClass() +", "+ ex.getMessage()); 
     } 
     return imgVw; 
    } 

    // Create the thumbnail on the fly 
    private Bitmap getImage(int id) { 
     Bitmap thumb = MediaStore.Video.Thumbnails.getThumbnail(
       getContentResolver(), 
       id, MediaStore.Video.Thumbnails.MICRO_KIND, null); 
     return thumb; 
    } 

} 

登錄:

> > 01-30 14:08:32.241 11542-11542/ro.vrt.main I/Process: Sending signal. PID: 11542 SIG: 9 01-30 14:08:37.508 11671-11671/ro.vrt.main 
> W/System: ClassLoader referenced unknown path: 
> /data/app/ro.vrt.main-2/lib/x86 01-30 14:08:37.510 
> 11671-11671/ro.vrt.main I/fd: BootstrapApplication created. Android 
> package is ro.vrt.main, real application class is null. 01-30 
> 14:08:37.511 11671-11671/ro.vrt.main I/fd: Resource override is 
> /data/data/ro.vrt.main/files/studio-fd/right/resources.ap_ 01-30 
> 14:08:37.511 11671-11671/ro.vrt.main I/fd: Resource patch last 
> modified: 1454178687000 01-30 14:08:37.511 11671-11671/ro.vrt.main 
> I/fd: APK last modified: 1454178303000 < resource patch 01-30 
> 14:08:37.512 11671-11671/ro.vrt.main I/fd: Last code patch: 
> /data/data/ro.vrt.main/files/studio-fd/dex/slice_7_2c7a21c06f0b2310eadc38962f037e8d9bfeb1d7-classes.dex 
> 01-30 14:08:37.512 11671-11671/ro.vrt.main I/fd: APK last modified: 
> 1454178303000 < 1454178752000 code patch 01-30 14:08:37.512 
> 11671-11671/ro.vrt.main I/fd: Bootstrapping class loader with dex list 
> [/data/data/ro.vrt.main/files/studio-fd/dex/slice_7_2c7a21c06f0b2310eadc38962f037e8d9bfeb1d7-classes.dex, 
> /data/data/ro.vrt.main/files/studio-fd/dex/slice_5_19eb0e3297ec4866c77c2a0621402c73a2c55731-classes.dex, 
> /data/data/ro.vrt.main/files/studio-fd/dex/slice_3_c9e402f5905d7d75403d2e682e8e8f0378a74371-classes.dex] 
> 01-30 14:08:37.512 11671-11671/ro.vrt.main I/fd: Incremental dex path 
> is 
> /data/data/ro.vrt.main/files/studio-fd/dex/slice_7_2c7a21c06f0b2310eadc38962f037e8d9bfeb1d7-classes.dex:/data/data/ro.vrt.main/files/studio-fd/dex/slice_5_19eb0e3297ec4866c77c2a0621402c73a2c55731-classes.dex:/data/data/ro.vrt.main/files/studio-fd/dex/slice_3_c9e402f5905d7d75403d2e682e8e8f0378a74371-classes.dex 
> 01-30 14:08:37.525 11671-11671/ro.vrt.main W/System: ClassLoader 
> referenced unknown path: /data/data/ro.vrt.main/files/studio-fd/lib 
> 01-30 14:08:37.529 11671-11671/ro.vrt.main I/fd: Starting server 
> socket listening for package ro.vrt.main on 
> [email protected] 01-30 14:08:37.533 
> 11671-11671/ro.vrt.main I/fd: Started server for package ro.vrt.main 
> 01-30 14:08:37.692 11671-11671/ro.vrt.main D/AndroidRuntime: Shutting 
> down VM 
>                
>                
>                --------- beginning of crash 01-30 14:08:37.692 11671-11671/ro.vrt.main 
> E/AndroidRuntime: FATAL EXCEPTION: main 
>                Process: ro.vrt.main, PID: 11671 
>                java.lang.RuntimeException: Unable to start activity 
> ComponentInfo{ro.vrt.main/ro.vrt.main.main}: 
> java.lang.NullPointerException: Attempt to get length of null array 
>                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
>                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
>                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
>                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
>                 at android.os.Handler.dispatchMessage(Handler.java:102) 
>                 at android.os.Looper.loop(Looper.java:148) 
>                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
>                 at java.lang.reflect.Method.invoke(Native Method) 
>                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
>                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
>                Caused by: java.lang.NullPointerException: Attempt to get length of null 
> array 
>                 at ro.vrt.main.main$VideoGalleryAdapter.getCount(main.java:142) 
>                 at android.widget.GridView.setAdapter(GridView.java:206) 
>                 at ro.vrt.main.main.setGalleryAdapter(main.java:56) 
>                 at ro.vrt.main.main.onCreate(main.java:51) 
>                 at android.app.Activity.performCreate(Activity.java:6237) 
>                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
>                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
>                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
>                 at android.app.ActivityThread.-wrap11(ActivityThread.java)  
>                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
>                 at android.os.Handler.dispatchMessage(Handler.java:102)  
>                 at android.os.Looper.loop(Looper.java:148)  
>                 at android.app.ActivityThread.main(ActivityThread.java:5417)  
>                 at java.lang.reflect.Method.invoke(Native Method)  
>                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
>                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  01-30 
> 14:08:44.290 11671-11671/ro.vrt.main I/Process: Sending signal. PID: 
> 11671 SIG: 9 01-30 14:09:23.786 11878-11878/ro.vrt.main I/Process: 
> Sending signal. PID: 11878 SIG: 9 

和main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <GridView 
     android:id="@+id/videoGrdVw" 
     android:numColumns="auto_fit" 
     android:gravity="center" 
     android:columnWidth="80dp" 
     android:stretchMode="columnWidth" 
     android:verticalSpacing="5dip" 
     android:horizontalSpacing="5dip" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

</GridView> 
</LinearLayout> 

回答

1

當你在那個時候將該適配器分配給您的ListView _videosId爲空,因爲到那時數據庫查詢尚未完成。數據準備就緒後,您需要設置適配器。

因爲你事先不知道數組的大小;你甚至不能用空白數據初始化數組。

解決方案

從方法刪除setGalleryAdapter();onCreate

而且

setGalleryAdapter();for循環之後方法initVideosId()

+0

沒有收到錯誤,但不顯示列表。好像它沒有找到電影SD卡 –

+0

@diaconuliviu:那是一個完全不同的問題。可能你應該爲此發表一個不同的問題。 – Rohit5k2

+0

好!非常感謝你 –