Android - 无法对非静态方法进行静态引用

时间:2014-01-07 14:41:39

标签: android multithreading

我重构我的线程以避免内存泄漏,我在处理程序中遇到2个错误,并且从线程内部调用startActivityForResult(处理GoogleDrive)

我有我的DownloadActivity:

公共类DownloadActivity扩展了Activity {         ....         private void getFolderId(){         getFolderIdThread = new GetFolderIdThread();         getFolderIdThread.start();     }

    private static class GetFolderIdThread extends Thread {
    private Boolean mRunning = false;

    @Override
    public void run() {
        mRunning = true;

                    fResultList = new ArrayList<File>();
            Files f1 = mService.files();
            Files.List request = null;
            aFolderId = null;
            do {
                try  {
                    request = f1.list();
                    String aQuery = "'root' in parents and mimeType='application/vnd.google-apps.folder' and title='"+ aFolderName + "'";
                    request.setQ(aQuery);                       
                    FileList fileList = request.execute();
                    fResultList.addAll(fileList.getItems());
                    request.setPageToken(fileList.getNextPageToken());
                } catch (UserRecoverableAuthIOException e) {
                    startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);  <=  THIS RAISES THE ERROR
                } catch (IOException e) {
                    e.printStackTrace();
                    if (request != null){
                        request.setPageToken(null);
                    }
                }
            } while (request.getPageToken() !=null && request.getPageToken().length() > 0);

            if (fResultList.size() == 0) {
                Log.d(TAG, "cannot find the training folder at root level");
                Message msg = handler.obtainMessage(); <=  THIS RAISES THE ERROR
                    Bundle bundle = new Bundle();
                    bundle.putInt("msgKey", DownloadActivity.NO_TRAININGS_FOLDER);
                           msg.setData(bundle);
                           handler.sendMessage(msg);    <=  THIS RAISES THE ERROR   
            } else {
                File folder = fResultList.get(0);
                aFolderId =  folder.getId();
                getFolderContents();  <=  THIS RAISES THE ERROR
        }
    }
    public void close() {
        mRunning = false;
    }
}

我在我的活动中定义了处理程序

Handler handler = new Handler() {
        @Override
    public void handleMessage(Message msg) {
        ...
        }
    }

和onActivityResult

    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    switch (requestCode) {
        case REQUEST_ACCOUNT_PICKER:
                    ....
                    break;
             }
    }

我可以选择绕过此错误吗?

1 个答案:

答案 0 :(得分:0)

您的GetFolderIdThread类是静态的,静态嵌套类不能引用创建它的外部类的实例中的非静态方法和字段。这样的嵌套类只能访问Activity中的静态方法和字段。从类定义中删除静态,我认为您的问题将得到解决。

您还需要在UI线程上发布对startActivityForResult的调用。当然,您应该能够使用您的处理程序,例如:

handler.post(new Runnable()
{
    public void run()
    {
        startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
    }
});

确保您的线程可以正常完成,因为它将继续运行。