如果我使用代码1
Utility.DetailsInfo myInfo=new Utility.DetailsInfo()导致错误,为什么?
No enclosing instance of type Utility is accessible. Must qualify the allocation with an enclosing instance of type Utility(例如x.new A(),其中x是Utility的实例)。

所以我必须使用代码2
我可以创建内部静态类的实例吗?谢谢!

代码1

public class Utility {

    class DetailsInfo {
        String title;
        String path;
        String size;
        Bitmap thumbnails;
    }

    public static void GetPhotoDetailsByID(Context mycontext,String id,DetailsInfo myInfo ){
             Cursor cur = mycontext.getContentResolver().query(
                     MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                     null,
                     MediaStore.Images.Media._ID+"=?",
                     new String[]{id},
                     "" );
             if (cur.moveToFirst()){
                 myInfo.title= cur.getString(cur.getColumnIndex(MediaStore.Images.Media.TITLE));
                 myInfo.path = cur.getString(cur.getColumnIndex(MediaStore.Images.Media.DATA));
                 myInfo.size=cur.getString(cur.getColumnIndex(MediaStore.Images.Media.SIZE));

                 myInfo.thumbnails = MediaStore.Images.Thumbnails.getThumbnail(
                                   mycontext.getContentResolver(),
                                   Integer.parseInt(id),
                                   MediaStore.Images.Thumbnails.MICRO_KIND,
                                   null);

             }
             cur.close();
        }
}


代码2

public class Utility {
    public static class DetailsInfo {
        String title;
        String path;
        String size;
        Bitmap thumbnails;
    }

    public static void GetPhotoDetailsByID(Context mycontext,String id,DetailsInfo myInfo ){
             Cursor cur = mycontext.getContentResolver().query(
                     MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                     null,
                     MediaStore.Images.Media._ID+"=?",
                     new String[]{id},
                     "" );
             if (cur.moveToFirst()){
                 myInfo.title= cur.getString(cur.getColumnIndex(MediaStore.Images.Media.TITLE));
                 myInfo.path = cur.getString(cur.getColumnIndex(MediaStore.Images.Media.DATA));
                 myInfo.size=cur.getString(cur.getColumnIndex(MediaStore.Images.Media.SIZE));

                 myInfo.thumbnails = MediaStore.Images.Thumbnails.getThumbnail(
                                   mycontext.getContentResolver(),
                                   Integer.parseInt(id),
                                   MediaStore.Images.Thumbnails.MICRO_KIND,
                                   null);

             }
             cur.close();
        }
}

最佳答案

希望这对您有帮助。

该行将导致错误,因为您必须使用其父类的对象来调用内部类。

Utility.DetailsInfo myInfo=new Utility.DetailsInfo();


仅当子类不是静态的时,才可以使用其父类对象来引用它。

喜欢:

Utility utility = new Utility();
Utility.DetailsInfo myInfo= new utility.DetailsInfo();


是的,您可以直接将内部类的实例声明为static。

07-27 23:45