2015-12-26 68 views
0

我正在做一個使用Android的Evernote的SDK的項目。我試圖從我的筆記(onItemClick)中獲取內容,但每次使用該方法時,應用程序都會崩潰,並顯示一個線程異常。 這是代碼的部分,我使用的方法:Evernote getNoteContent()Asynctask錯誤

public void onFragmentInteraction(Note note) throws EDAMUserException, EDAMSystemException, EDAMNotFoundException, TException, ExecutionException, InterruptedException { 
    String noteGuid = note.getGuid(); 
    String Content = new GetNoteTask().execute(noteGuid).get(); 
    Intent intent = new Intent(this, ViewNoteActivity.class); 
    intent.putExtra(intent.EXTRA_TEXT, Content); 
    startActivity(intent); 

} 

public class GetNoteTask extends AsyncTask<String, Void, String> { 
private EvernoteSession mEvernoteSession; 


public String noteContent(String guid) throws EDAMUserException, EDAMSystemException, TException, EDAMNotFoundException { 
    final EvernoteNoteStoreClient noteStoreClient = mEvernoteSession.getEvernoteClientFactory().getNoteStoreClient(); 
     String noteText; 
     noteText = noteStoreClient.getNoteContent(guid); 
     return noteText; 
    } 

@Override 
protected String doInBackground(String... params) { 
    String guid = params[0]; 
    String noteContent = null; 
    try { 
     noteContent = noteContent(guid); 
    } catch (EDAMUserException e) { 
     e.printStackTrace(); 
    } catch (EDAMSystemException e) { 
     e.printStackTrace(); 
    } catch (TException e) { 
     e.printStackTrace(); 
    } catch (EDAMNotFoundException e) { 
     e.printStackTrace(); 
    } 
    return noteContent; 
} 

}

第一種方法是在位於我的MainActivity延伸的片段。第二個是我的任務來獲取內容說明。當我向asynctask中的Evernote客戶開放時,它會中斷。

final EvernoteNoteStoreClient noteStoreClient = mEvernoteSession.getEvernoteClientFactory().getNoteStoreClient(); 
     String noteText; 

謝謝大家提前,並希望你能幫助我!

回答

0

我已經找到解決方案。我不能在沒有在我的主要活動中聲明的mEvernoteSession的情況下調用EvernoteClient,因此我試圖發送noteStoreClient,但它始終是空的,因爲該方法是私人的,並且沒有受到保護,所以當我發送它時沒有填充它。

最後,我創建了一個靜態類,其中保存我的變量:

public static class MyTaskParams{ 
     public String guid; 
     public EvernoteNoteStoreClient noteStore; 


    public MyTaskParams(String noteGuid, EvernoteNoteStoreClient noteStoreClient) { 
     this.guid=noteGuid; 
     this.noteStore=noteStoreClient; 
    } 

我sended後來到的AsyncTask。

MyTaskParams params = new MyTaskParams(noteGuid,noteStoreClient); 

     GetNoteTask GetNoteTask = new GetNoteTask(); 

所以最後我的AsyncTask的樣子:

public class GetNoteTask extends AsyncTask<MainActivity.MyTaskParams,Void , String> { 

public String noteContent(EvernoteNoteStoreClient noteStoreClient, String guid) throws EDAMUserException, EDAMSystemException, TException, EDAMNotFoundException { 
     String noteText; 
     noteText = noteStoreClient.getNoteContent(guid); 
     return noteText; 
    } 


protected String doInBackground(MainActivity.MyTaskParams... params) { 

    String noteContent = null; 
    String guid = params[0].guid; 
    EvernoteNoteStoreClient noteStoreClient = params[0].noteStore; 
    try { 

     noteContent = noteContent(noteStoreClient, guid); 
    } catch (EDAMUserException e) { 
     e.printStackTrace(); 
    } catch (EDAMSystemException e) { 
     e.printStackTrace(); 
    } catch (TException e) { 
     e.printStackTrace(); 
    } catch (EDAMNotFoundException e) { 
     e.printStackTrace(); 
    } 
    return noteContent; 
} 

謝謝大家,希望這將有助於有人在未來!