-1
を初期化された場合でも、ドライブのAPIを呼び出すためにしようとしたとき、私はGoogleドライブに画像を保存しようとすると、私は、次のNullポインタ例外を取得:NULLポインタ例外参照が
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を初期化してから、検索メソッドでファイルを取得しようとします。
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();
}
[Null Pointer Exceptionとは何ですか?それを修正するにはどうすればいいですか?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how -do-i-fix-it) – Gendarme
GDUT.AM.getActiveEmil()がnullを返さないことをテストしましたか? – RubioRic
はい、getActiveEmilメソッドがnullを返さないことをテストしました。現在、有効なメールを返します。 – ap6491