我正在构建一个Android应用程序。我已经为数据库的备份特定表数据编写代码,并在谷歌驱动器的应用程序商店中推送谷歌驱动器。
它完美地适用于调试模式,但是当我构建一个发布APK时,它会给我错误。这是请求" file.getId()"。
的空指针以下是代码
private class MakeBackUp extends AsyncTask<Void, Void, List<String>> {
private Exception mLastError = null;
MakeBackUp(GoogleAccountCredential credential) {
Log.e("Error", "runnung4");
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
mService = new com.google.api.services.drive.Drive.Builder(
transport, jsonFactory, credential)
.setApplicationName(getApplicationContext().getPackageName())
.build();
}
/**
* Background task to call Drive API.
*
* @param params no parameters needed for this task.
*/
@Override
protected List<String> doInBackground(Void... params) {
Log.e("Error", "runnung3");
try {
return getDataFromApi();
} catch (Exception e) {
mLastError = e;
Log.e("Error1", "errorPhase: " + mLastError);
if (mLastError instanceof UserRecoverableAuthIOException) {
Log.e("Error", "runnung4.5.3");
startActivityForResult(
((UserRecoverableAuthIOException) mLastError).getIntent(),
BackupAndRestoreMessage.REQUEST_AUTHORIZATION);
}
cancel(true);
onCancelled();
return null;
}
}
/**
* Fetch a list of up to 10 file names and IDs.
*
* @return List of Strings describing files, or an empty list if no files
* found.
* @throws IOException
*/
private List<String> getDataFromApi() throws IOException {
// Get a list of up to 10 files.
List<String> fileInfo = new ArrayList<String>();
FileList result = mService.files().list()
.setPageSize(10)
.setFields("nextPageToken, files(id, name)")
.execute();
List<File> files = result.getFiles();
if (files != null) {
for (File file : files) {
fileInfo.add(String.format("%s (%s)\n",
file.getName(), file.getId()));
}
}
// upload db
try {
Log.e("Error", "runnung");
File fileMetadata = new File();
fileMetadata.setName("file_"+ System.currentTimeMillis() + ".json");
fileMetadata.setParents(Collections.singletonList("appDataFolder"));
Log.e("Error", "runnung0.1");
java.io.File filePath = new java.io.File(getFilePath.BackupSMS(getApplicationContext()));
Log.e("Error", "runnung0.2");
FileContent mediaContent = new FileContent("application/json", filePath);
Log.e("File_Size", Integer.parseInt(String.valueOf(mediaContent.getLength() / 1024)) + "");
Log.e("Error", "runnung0.3");
File file = mService.files().create(fileMetadata, mediaContent)
.setFields("id")
.execute();
Log.e("Error", "runnung0.4");
try {
Log.e("Error File ID: ", file.getId()); // System.out.println("File ID: " + file.getId());
editor1.putInt("fileSize", Integer.parseInt(String.valueOf(mediaContent.getLength() / 1024))).apply();
editor1.putString("fileDate", System.currentTimeMillis() + "").apply();
}
catch (Exception e)
{
Log.e("Error0_", e.getMessage());
}
Log.e("Error", "runnung0.5");
} catch (Exception e) {
e.printStackTrace();
Log.e("Error0", e.getMessage());
}
return null;
}
@Override
protected void onPreExecute() {
mProgress = new ProgressDialog(BackupAndRestoreMessage.this);
mProgress.setCanceledOnTouchOutside(false);
mProgress.setCancelable(false);
mProgress.show();
mProgress.setMessage(Html.fromHtml("<font color='#000000'>Creating local db dump</font>"));
Log.e("Error", "runnung1");
}
@Override
protected void onPostExecute(List<String> output) {
Log.e("Error", "runnung2");
if (pref.getString("fileDate", null) != null) {
backupTym_size.setTextColor(getResources().getColor(R.color.grey));
backupTym_size.startAnimation(out);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
backupTym_size.setTextColor(getResources().getColor(R.color.black));
if (DateTimeDifferent(pref.getString("fileDate", null)) >= thisYearTotalDays()) {
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
Date date = new Date(Long.parseLong(pref.getString("fileDate", null)));
if (darkTheme) {
backupTym_size.setTextColor(getResources().getColor(R.color.white));
} else {
backupTym_size.setTextColor(getResources().getColor(R.color.black));
}
backupTym_size.setText("Google Drive: " + dateFormat.format(date) + "\nSize: " + pref.getInt("fileSize", 0) + " KB");
} else {
if (darkTheme) {
backupTym_size.setTextColor(getResources().getColor(R.color.white));
} else {
backupTym_size.setTextColor(getResources().getColor(R.color.black));
}
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
dateFormat = new SimpleDateFormat("dd/MM hh:mm a");
Date date = new Date(Long.parseLong(pref.getString("fileDate", null)));
backupTym_size.setText("Google Drive: " + dateFormat.format(date) + "\nSize: " + pref.getInt("fileSize", 0) + " KB");
}
}
}, 3000);
snackbar("Backup completed successfully");
}
if (!BackupAndRestoreMessage.this.isFinishing()) {
mProgress.dismiss();
}
}
@Override
protected void onCancelled() {
Log.e("Error", "runnung4.5.5");
if (!BackupAndRestoreMessage.this.isFinishing()) { // or call isFinishing() if min sdk version < 17
mProgress.dismiss();
}
if (mLastError != null) {
Log.e("Error", "runnung4.5.1");
mProgress.hide();
if (mLastError instanceof GooglePlayServicesAvailabilityIOException) {
Log.e("Error", "runnung4.5.2");
showGooglePlayServicesAvailabilityErrorDialog(
((GooglePlayServicesAvailabilityIOException) mLastError)
.getConnectionStatusCode());
mProgress.hide();
}
{
else {
Log.e("Error", "runnung4.5.4");
}
} else {
Log.e("Error", "runnung4.5.6");
}
}
}
以下是我的程序代码
release {
minifyEnabled true
proguardFile 'proguard-android.txt'
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
}
以下是依赖
compile 'com.google.android.gms:play-services-analytics:11.6.2'
compile 'com.google.android.gms:play-services-auth:11.6.2'
compile 'pub.devrel:easypermissions:0.3.0'
compile('com.google.api-client:google-api-client-android:1.23.0') {
exclude group: 'org.apache.httpcomponents'
}
compile('com.google.apis:google-api-services-drive:v3-rev102-1.23.0') {
exclude group: 'org.apache.httpcomponents'
}
compile 'com.android.support:multidex:1.0.1'