尝试调用驱动器API时出现空指针异常,即使在初始化引用时也是如此



当我试图在Google Drive中保存图像时,会出现以下空指针异常:

Attempt to invoke virtual method 'com.google.api.services.drive.Drive$Files com.google.api.services.drive.Drive.files()' on a null object reference
                          java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.api.services.drive.Drive$Files com.google.api.services.drive.Drive.files()' on a null object reference
                              at com.amrutpatil.makeanote.GDActions.search(GDActions.java:208)
                              at com.amrutpatil.makeanote.NotesActivity$2.run(NotesActivity.java:154)

GDActions.java:

我首先在init方法中初始化GoogleApiClient,然后尝试在search方法中获取文件。

static void init(NotesActivity ctx, String email) {
        if (ctx != null && email != null) {
            mGAC = new GoogleApiClient.Builder(ctx).addApi(Drive.API)
                    .addScope(Drive.SCOPE_FILE).setAccountName(email)
                    .addConnectionCallbacks(ctx).addOnConnectionFailedListener(ctx).build();
            mGOOSvc = new com.google.api.services.drive.Drive.Builder(
                    AndroidHttp.newCompatibleTransport(), new GsonFactory(), GoogleAccountCredential
                    .usingOAuth2(ctx, Arrays.asList(DriveScopes.DRIVE_FILE))
                    .setSelectedAccountName(email)
            ).build();
        }
    }
static ArrayList<GF> search(String prId, String titl, String mime) {
            ArrayList<GF> gfs = new ArrayList<>();
            String qryClause = "'me' in owners and ";
            if (prId != null) qryClause += "'" + prId + "' in parents and ";
            if (titl != null) qryClause += "title = '" + titl + "' and ";
            if (mime != null) qryClause += "mimeType = '" + mime + "' and ";
            qryClause = qryClause.substring(0, qryClause.length() - " and ".length());
            com.google.api.services.drive.Drive.Files.List qry = null;
            try {
                qry = mGOOSvc.files().list().setQ(qryClause)
                        .setFields("items(id, labels/trashed, title), nextPageToken");   //Error here
                gfs = search(gfs, qry);
            } catch (GoogleAuthIOException gaiEx) {
                try {
                    gfs = search(gfs, qry);
                    gaiEx.printStackTrace();
                } catch (Exception g) {
                    GDUT.le(g);
                }
            } catch (Exception e) {
                GDUT.le(e);
            }
            return gfs;
        }

NotesActivity.java

@Override
    public void onLoadFinished(Loader<List<Note>> loader, List<Note> data) {
        this.mNotes = data;
        Thread[] threads = new Thread[mNotes.size()];
        int threadCounter = 0;
        for(final Note aNote : mNotes){
            if(AppConstant.GOOGLE_DRIVE_SELECTION == aNote.getStorageSelection()){
                GDUT.init(this);
                if(checkPlayServices() && checkUserAccount()){
                    GDActions.init(this, GDUT.AM.getActiveEmil());
                    GDActions.connect(true);
                }
                threads[threadCounter] = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        do{
                            //Get the image file
                            ArrayList<GDActions.GF> gfs = GDActions.search(AppSharedPreferences.getGoogleDriveResourceId(getApplicationContext()),
                                    aNote.getImagePath(), GDUT.MIME_JPEG);   //Error here
                            if(gfs.size() > 0){
                                //Retrieve the file, convert it into Bitmap to display on the screen
                                byte[] imageBytes = GDActions.read(gfs.get(0).id, 0);

                                Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
                                aNote.setBitmap(bitmap);
                                mIsImageNotFound = false;
                                mNotesAdapter.setData(mNotes);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        mNotesAdapter.notifyImageObtained();
                                    }
                                });
                            } else{
                                aNote.setBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_loading));
                                mIsImageNotFound = true;
                                try{
                                    Thread.sleep(500);
                                } catch (InterruptedException e){
                                    e.printStackTrace();
                                }
                            }
                        }while(mIsImageNotFound);
                    }
                });
                threads[threadCounter].start();
                threadCounter++;
            } else {
                aNote.setHasNoImage(true);
            }
        }
        mNotesAdapter.setData(mNotes);  
        changeNoItemTag();
    }

发现存在权限问题。尽管我在AndroidManifest.xml文件中设置了GET_ACCOUNTS权限,但我需要在应用程序设置中手动启用联系人权限。

这可能是一个特定于设备的问题,因为我在Nexus 6物理设备上使用Android棉花糖6.0.1运行我的应用程序。

为了安全起见,您可能需要检查应用程序设置。

最新更新