0

我正在尝试将文件上传到保管箱,我还创建了一个进度条来检查其状态,但在执行时会引发异常。我创建了一个 UploadFile 类的对象并调用 UploadFile 函数,传递的参数是 UploadData.this, mApi,FILE_DIR,file。

public class UploadFile extends AsyncTask<Void, Long, Boolean> {
private DropboxAPI<?> mApi;
private String mPath;
private File mFile;
private long mFileLen;
private UploadRequest mRequest;
private Context mContext;
private final ProgressDialog mDialog;
private String mErrorMsg;
public UploadFile(Context context, DropboxAPI<?> api, String dropboxPath,
        File file) 
{
    mContext = context.getApplicationContext();
    mFileLen = file.length();
    mApi = api;
    mPath = dropboxPath;
    mFile = file;

    mDialog = new ProgressDialog(context);
    mDialog.setMax(100);
    mDialog.setMessage("Uploading " + file.getName());
    mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    mDialog.setProgress(0);
    mDialog.setButton("Cancel", new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // This will cancel the putFile operation
            mRequest.abort();
        }
    });
    mDialog.show();
}

@Override
protected Boolean doInBackground(Void... params) {
    try {
        FileInputStream fis = new FileInputStream(mFile);
        String path = mPath + mFile.getName();
        mRequest = mApi.putFileOverwriteRequest(path, fis, mFile.length(),
                new ProgressListener() {
            @Override
            public long progressInterval() {
                return 500;
            }
            public void onProgress(long bytes, long total) {
                publishProgress(bytes);
            }
        });

        if (mRequest != null) {
            mRequest.upload();
            return true;
        }

    } catch (DropboxUnlinkedException e) {
        mErrorMsg = "This app wasn't authenticated properly.";
    } catch (DropboxFileSizeException e) {
        mErrorMsg = "This file is too big to upload";
    } catch (DropboxPartialFileException e) {
        mErrorMsg = "Upload canceled";
    } catch (DropboxServerException e) {
        if (e.error == DropboxServerException._401_UNAUTHORIZED) {
        } else if (e.error == DropboxServerException._403_FORBIDDEN) {
        } else if (e.error == DropboxServerException._404_NOT_FOUND) {

        } else if (e.error == DropboxServerException._507_INSUFFICIENT_STORAGE) {
            // user is over quota
        } else {
            // Something else
        }
        // This gets the Dropbox error, translated into the user's language
        mErrorMsg = e.body.userError;
        if (mErrorMsg == null) {
            mErrorMsg = e.body.error;
        }
    } catch (DropboxIOException e) {

        mErrorMsg = "Network error.  Try again.";
    } catch (DropboxParseException e) {
        mErrorMsg = "Dropbox error.  Try again.";
    } catch (DropboxException e) {
        mErrorMsg = "Unknown error.  Try again.";
    } catch (FileNotFoundException e) {
    }
    return false;
}

@Override
protected void onProgressUpdate(Long... progress) {
    int percent = (int)(100.0*(double)progress[0]/mFileLen + 0.5);
    mDialog.setProgress(percent);
}

@Override
protected void onPostExecute(Boolean result) {
    mDialog.dismiss();
    if (result) {
        showToast("Image successfully uploaded");
    } else {
        showToast(mErrorMsg);
    }
}

private void showToast(String msg) {
    Toast error = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
    error.show();
}
}

这是日志(我是新手,我不正确理解日志)

06-30 06:18:34.400: W/dalvikvm(422): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
06-30 06:18:34.410: E/AndroidRuntime(422): FATAL EXCEPTION: AsyncTask #1
06-30 06:18:34.410: E/AndroidRuntime(422): java.lang.RuntimeException: An error occured while executing doInBackground()
06-30 06:18:34.410: E/AndroidRuntime(422):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
06-30 06:18:34.410: E/AndroidRuntime(422):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-30 06:18:34.410: E/AndroidRuntime(422):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-30 06:18:34.410: E/AndroidRuntime(422):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-30 06:18:34.410: E/AndroidRuntime(422):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-30 06:18:34.410: E/AndroidRuntime(422):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
06-30 06:18:34.410: E/AndroidRuntime(422):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
06-30 06:18:34.410: E/AndroidRuntime(422):  at java.lang.Thread.run(Thread.java:1096)
06-30 06:18:34.410: E/AndroidRuntime(422): Caused by: java.lang.NullPointerException
06-30 06:18:34.410: E/AndroidRuntime(422):  at com.sachin.UploadFile.doInBackground(UploadFile.java:59)
06-30 06:18:34.410: E/AndroidRuntime(422):  at com.sachin.UploadFile.doInBackground(UploadFile.java:1)
06-30 06:18:34.410: E/AndroidRuntime(422):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-30 06:18:34.410: E/AndroidRuntime(422):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
4

2 回答 2

1

确保正确初始化

UploadRequest mApi;

因为不这样做会导致NullPointerException方法onClick被调用。

于 2012-06-30T01:44:38.860 回答
0

在有异常的行上放置一个断点并调试您的应用程序。当它停在那里时,检查您在该行上使用的所有变量值。如果一个或多个为空,则存在您的错误。这些是您在该方法中使用之前忘记初始化的变量。

于 2012-06-30T02:27:26.160 回答