问题在于标题,有人可以帮助我吗? stackoverflow上的其他类似问题对我来说并不起作用。 Android Studio告诉我"方法不会覆盖来自超类的方法。",因此,我正在通过stackflow尝试不同的解决方案,例如:
我的代码:
public class MainActivity extends AppCompatActivity {
EditText downloadText;
ProgressBar progressBar;
TextView progressText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
downloadText = findViewById(R.id.downloadLink);
progressBar = findViewById(R.id.progressBar);
progressBar.setMax(100);
progressBar.setIndeterminate(false);
progressText = findViewById(R.id.progressText);
if(shouldAskPermissions()) {
askPermissions();
}
}
public void startDownload(View view) {
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute(downloadText.getText().toString());
}
public void setProgressText(int percentage) {
progressText.setText("Download bei " + percentage + "%!");
}
protected boolean shouldAskPermissions() { //Quelle: https://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android
return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
}
@TargetApi(23)
protected void askPermissions() { //Quelle: https://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android
String[] permissions = {
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE"
};
int requestCode = 200;
requestPermissions(permissions, requestCode);
}
static class DownloadFile extends AsyncTask<String, Integer, String> { // Quelle: https://www.androidhive.info/2012/04/android-downloading-file-by-showing-progress-bar/
@Override
public String doInBackground(String... sUrl) {
try {
URL url = new URL(sUrl[0]);
URLConnection connection = url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()+"/" + "Test.jpg");
byte data[] = new byte[8192];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
System.out.println("Download beendet!");
output.flush();
output.close();
input.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
}
@Override
protected void onProgressUpdate(Integer... progress) {
progressBar.setProgress(progress[0]);
setProgressText(progress[0]);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(this, "Download beendet!", Toast.LENGTH_LONG).show();
}
}
提前致谢。
答案 0 :(得分:2)
你只需要在onProgressUpdate()之前删除括号“}”并最后添加这个“}”:
由于这种括号方法(progressUpdate()
和postExecute()
)没有包含在AsyncTask中,它们被包含在Activity类中,而Activity.class没有这些覆盖方法,因此它显示错误。
static class DownloadFile extends AsyncTask<String, Integer, String> { // Quelle: https://www.androidhive.info/2012/04/android-downloading-file-by-showing-progress-bar/
@Override
public String doInBackground(String... sUrl) {
try {
URL url = new URL(sUrl[0]);
URLConnection connection = url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/" + "Test.jpg");
byte data[] = new byte[8192];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
System.out.println("Download beendet!");
output.flush();
output.close();
input.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
// ---- comment this
// }
@Override
protected void onProgressUpdate(Integer... progress) {
progressBar.setProgress(progress[0]);
setProgressText(progress[0]);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(this, "Download beendet!", Toast.LENGTH_LONG).show();
}
//----- Add extra } here
}