Java 使用服务下载文件

Java 使用服务下载文件,java,android,Java,Android,我有一个应用程序,可以从带有ProgressDialog的服务器下载apk文件,并尝试更新该文件。但是,如果单击“下载”按钮,应用程序崩溃,并在下面的行中出现错误,下载无法启动。如果我删除了ProgressDialog的所有代码,下载也不会启动并导致应用程序崩溃。为什么?请帮帮我 progressDialog.show(); 我的DownloadService.java public class DownloadService extends IntentService { publi

我有一个应用程序,可以从带有
ProgressDialog
的服务器下载apk文件,并尝试更新该文件。但是,如果单击“下载”按钮,应用程序崩溃,并在下面的行中出现错误,下载无法启动。如果我删除了
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>

您尚未在
新应用程序
活动中初始化
进度对话框
。因此,在
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);

                    }
        });
    }
}

请发布您的logcat.done..plz check这是一个
NullPointerException
,表明您的
ProgressDialog
未初始化。如何解决?很高兴听到我能提供帮助。谢谢
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);

                    }
        });
    }
}