Evernote getNoteContent()Asynctask错误

时间:2015-12-26 20:32:47

标签: java android sdk evernote

我正在使用适用于Android的Evernote SDK进行proyect。我试图从我的一个笔记(onItemClick)获取内容,但每次我使用该方法时,应用程序都会因为线程异常而崩溃。 这是我使用该方法的代码的一部分:

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中,它扩展了Fragment。第二个是获取内容备注的任务。当我在asynctask中使用Evernote客户端时,它会中断。

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

提前感谢大家,希望你能帮助我!

1 个答案:

答案 0 :(得分: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;
    }

我稍后将其发送给了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;
}