我有从服务器下载 apk 文件的应用程序ProgressDialog
,并尝试更新。但是如果单击按钮下载应用程序崩溃并在以下行中出现错误并且下载不会开始。如果我删除ProgressDialog
也下载的所有代码不会启动并导致应用程序崩溃。为什么?请帮帮我。
progressDialog.show();
我的 DownloadService.java
public class DownloadService extends IntentService {
public static final int UPDATE_PROGRESS = 8344;
public DownloadService() {
super("DownloadService");
}
@Override
protected void onHandleIntent(Intent intent) {
String urlToDownload = intent.getStringExtra("url");
ResultReceiver receiver = (ResultReceiver) intent.getParcelableExtra("receiver");
try {
URL url = new URL(urlToDownload);
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a typical 0-100% progress bar
int fileLength = connection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(connection.getInputStream());
OutputStream output = new FileOutputStream("/sdcard/debug.apk");
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
Bundle resultData = new Bundle();
resultData.putInt("progress" ,(int) (total * 100 / fileLength));
receiver.send(UPDATE_PROGRESS, resultData);
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
Bundle resultData = new Bundle();
resultData.putInt("progress" ,100);
receiver.send(UPDATE_PROGRESS, resultData);
}
}
而且活动是..
public class new_app extends AppCompatActivity {
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_app);
Button down2 = (Button) findViewById(R.id.down);
down2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
progressDialog.show();
Intent intent = new Intent(Emoji11ios.this, DownloadService.class);
intent.putExtra("url", "https://dl.dropboxusercontent.com/s/14rzjsl26zf0mqf/SCR%20Screen%20Recorder%20Pro%20V0.21.7.apk");
intent.putExtra("receiver", new DownloadReceiver(new Handler()));
startService(intent);
}
});
}
private class DownloadReceiver extends ResultReceiver {
public DownloadReceiver(Handler handler) {
super(handler);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
if (resultCode == DownloadService.UPDATE_PROGRESS) {
int progress = resultData.getInt("progress");
progressDialog.setProgress(progress);
if (progress == 100) {
progressDialog.dismiss();
}
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==android.R.id.home)
finish();
return super.onOptionsItemSelected(item);
}
}
这是 AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true">
<activity android:name=".maActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".preview_update"
android:theme="@style/AppTheme_new" />
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".DownloadService" >
</service>
</application>
最后是logcat
03-24 22:13:30.445 2166-2166/? E/Zygote: isWhitelistProcess - Process is Whitelisted
03-24 22:13:30.446 2166-2166/? E/libpersona: scanKnoxPersonas
03-24 22:13:30.446 2166-2166/? E/libpersona: Couldn't open the File - /data/system/users/0/personalist.xml - No such file or directory
03-24 22:13:36.558 2166-2166/com.testapp4s.down E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.testapp4s.down, PID: 2166
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ProgressDialog.show()' on a null object reference
at com.testapp4s.down.preview_update.new_app $2.onClick(new_app .java:116)
at android.view.View.performClick(View.java:6896)
at android.widget.TextView.performClick(TextView.java:12651)
at android.view.View$PerformClick.run(View.java:26088)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6940)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
你还没有ProgressDialog
在你的new_app
活动中初始化你的。所以在你的onCreate
函数中,你需要像下面这样初始化它。检查您需要初始化它的注释。
public class new_app extends AppCompatActivity {
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_app);
// You need to initialize it here.
progressDialog = new ProgressDialog(this);
Button down2 = (Button) findViewById(R.id.down);
down2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
progressDialog.show();
Intent intent = new Intent(Emoji11ios.this, DownloadService.class);
intent.putExtra("url", "https://dl.dropboxusercontent.com/s/14rzjsl26zf0mqf/SCR%20Screen%20Recorder%20Pro%20V0.21.7.apk");
intent.putExtra("receiver", new DownloadReceiver(new Handler()));
startService(intent);
}
});
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句