마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

Attempt to get length of null array 오류 질문입니다.

0 추천

앱 내 갤러리를 따로 만들려고 코드를 찾아 해봤는데 null array 오류라고 하네요ㅠ

CustomGalleryAdapter.java 중에 getCount의 mImgs.length에서 오류가 발생했습니다ㅠ

public class CustomGalleryAdapter extends BaseAdapter {
    int CustomGalleryItemBg;
    String mBasePath;
    Context mContext;
    String[] mImgs;
    Bitmap bm;

    public String TAG = "Gallery Adapter :: ";

    public CustomGalleryAdapter(Context mContext,String basepath) {
       this.mContext = mContext;
       this.mBasePath = basepath;

       File file = new File(mBasePath);
       if(!file.exists()) {
           if(!file.mkdirs()) {
               Log.d(TAG,"failed to create dir");
           }
       }
       mImgs = file.list();

        TypedArray array = mContext.obtainStyledAttributes(R.styleable.GalleryTheme);
        CustomGalleryItemBg = array.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground,0);
        array.recycle();
    }

    @Override
    public int getCount() {
        return mImgs.length;
    }

위의 activity.java에서는 customGallery.setAdapter(custom~); 여기서 문제가 생기네요ㅠadapter의 문제 때문이겠죠..도와주시면 감사하겠습니다..ㅠ
odette (150 포인트) 님이 2019년 2월 22일 질문

1개의 답변

0 추천
권한이 없을 경우 file.list(); 가 null이 반환될  수 있습니다.  

그래서 setAdapter 호출 시 내부에서 getCount 를 부르다 Exception이 발생된 것으로 생각되니, 확인 해 보세요.
익명사용자 님이 2019년 2월 22일 답변
Manifest.xml에 READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE, CAMERA 다 추가 되어 있는데 더 필요한 권한이 있을까요..?
...