我有这个问题,应用程序可以正常运行一两次,然后关闭,android studio告诉我:
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.inkshared.gamer_hd_wallpaper, PID: 17686
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ProgressDialog.dismiss()' on a null object reference
at com.inkshared.gamer_hd_wallpaper.ui.detail.WallpaperDetailFragment$DownloadFileAsync.onPostExecute(WallpaperDetailFragment.java:1453)
at com.inkshared.gamer_hd_wallpaper.ui.detail.WallpaperDetailFragment$DownloadFileAsync.onPostExecute(WallpaperDetailFragment.java:1366)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.-wrap1(Unknown Source:0)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6626)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
D/FA: Logging event (FE): app_exception(_ae), Bundle[{ga_event_origin(_o)=clx, _r=1, ga_screen_class(_sc)=AdActivity, ga_screen_id(_si)=-254988858715173777, timestamp=1572676684488, fatal=1}]
W/CrashlyticsCore: Cannot send reports. Settings are unavailable.
V/FA: Recording user engagement, ms: 1623
D/FA: Logging event (FE): user_engagement(_e), Bundle[{ga_event_origin(_o)=auto, engagement_time_msec(_et)=1623, ga_screen_class(_sc)=AdActivity, ga_screen_id(_si)=-254988858715173777}]
Process 17686 terminated.
据我所知,我是一个初学者,他指出了这两行:
@Override
protected void onPostExecute(String unused) {
mProgressDialog.dismiss();
dismissDialog();
progress.dismiss();
if (mProgressDialog.isShowing()) {
mProgressDialog.cancel();
progress.dismiss();
}
if (isSuccess) {
setDownloadCount(wallpaperViewModel.wallpapers.get(itemPosition).wallpaper_id);
Toast.makeText(getContext(), getString(R.string.message__download_success), Toast.LENGTH_LONG).show();
}
}
据我所知,我是一个初学者,他指出了以下几行:
--------------第1453行----------------
@Override
protected void onPostExecute(String unused) {
mProgressDialog.dismiss();
dismissDialog();
progress.dismiss();
if (mProgressDialog.isShowing()) {
mProgressDialog.cancel();
progress.dismiss();
}
if (isSuccess) {
setDownloadCount(wallpaperViewModel.wallpapers.get(itemPosition).wallpaper_id);
Toast.makeText(getContext(), getString(R.string.message__download_success), Toast.LENGTH_LONG).show();
}
}
--------------------第1366行----------------------- >
class DownloadFileAsync extends AsyncTask<String, String, String> {
private boolean isSuccess = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog();
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
String fileNameAndExtension = aurl[1];
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Accept-Encoding", "identity");
urlConnection.connect();
InputStream input = new BufferedInputStream(url.openStream());
String path = Environment.getExternalStorageDirectory().toString();
File dir = new File( path,"/HD WALLPAPERS/");
if (!dir.exists()) {
boolean b = dir.mkdir();
int i = 0;
}
File file = new File(dir, fileNameAndExtension);
String imagePath = file.getAbsolutePath();
//scan the image so show up in album
MediaScannerConnection.scanFile(getContext(),
new String[] { imagePath }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
try {
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
int lenghtOfFile = urlConnection.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
isSuccess = true;
} catch (Exception e) {
isSuccess = false;
Utils.psErrorLog("", e);
}
} catch (Exception e) {
isSuccess = false;
Utils.psErrorLog("", e);
}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC", progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
mProgressDialog.dismiss();
dismissDialog();
progress.dismiss();
if (mProgressDialog.isShowing()) {
mProgressDialog.cancel();
progress.dismiss();
}
if (isSuccess) {
setDownloadCount(wallpaperViewModel.wallpapers.get(itemPosition).wallpaper_id);
Toast.makeText(getContext(), getString(R.string.message__download_success), Toast.LENGTH_LONG).show();
}
}
}
我希望能很好地使用照片,感谢您的帮助。