【问题标题】:java.lang.NullPointerException:image not loading [duplicate]java.lang.NullPointerException:图像未加载[重复]
【发布时间】:2018-01-12 19:13:36
【问题描述】:

尝试调用虚拟方法“void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)”。 在空对象引用上。 我遇到这个错误请帮忙。

 private void loadSongs() {
        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
        Cursor cursor = getContext().getContentResolver().query(uri, null, selection, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {

                    String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                    String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                    String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                    String albumId = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                    Cursor cursorm = getContext().getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                            new String[] {MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
                            MediaStore.Audio.Albums._ID+ "=?",
                            new String[] {String.valueOf(albumId)},
                            null);
                    assert cursorm != null;
                    if (cursorm.moveToFirst()) {
                        String path = cursorm.getString(cursorm.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
                        Drawable img = Drawable.createFromPath(path);
                        image = (ImageView)rootView.findViewById(R.id.albumArt);
                       // image.setImageDrawable(img);
                        image.setImageDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.ed));
                    }
                    songInfo s = new songInfo(name, artist, url);
                    _songs.add(s);
                }
                while (cursor.moveToNext());
                cursor.close();
                recyclerView.getAdapter().notifyDataSetChanged();
            }
        }
    }

【问题讨论】:

    标签: android nullpointerexception android-mediaplayer


    【解决方案1】:

    看来这个表达式返回null

    image = (ImageView)rootView.findViewById(R.id.albumArt);
    

    如果image 为空,则下一行引发异常:

    image.setImageDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.ed));
    

    您必须找出(ImageView)rootView.findViewById(R.id.albumArt); 返回null 的原因并保证它不返回null 或者检查图像视图是否为空并为其提供默认值。

    【讨论】:

      【解决方案2】:

      据我了解方法 rootView.findViewById

      返回

      如果找到,则具有给定 ID 的视图,否则为 null

      错误信息的含义:

      未找到具有给定 id 的视图,因此 image = (ImageView) null; 并且图像为空;

      因此在尝试执行时:

      image.setImageDrawable(..)

      你得到一个 NRE。

      解决方案:

      确保 rootView.findViewById 找到了一些东西。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-03
        • 1970-01-01
        • 1970-01-01
        • 2013-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多