Java "最新进展"及;OnPostExecute不会被覆盖
问题出在标题上,有人能帮我吗? stackoverflow上的其他类似问题对我不起作用。 Android Studio告诉我“方法不会覆盖超类中的方法”。正因为如此,我经历了stackflow并尝试了不同的解决方案,如:Java "最新进展"及;OnPostExecute不会被覆盖,java,android,Java,Android,问题出在标题上,有人能帮我吗? stackoverflow上的其他类似问题对我不起作用。 Android Studio告诉我“方法不会覆盖超类中的方法”。正因为如此,我经历了stackflow并尝试了不同的解决方案,如: 将参数字符串、整数、字符串与我的方法匹配 尝试为方法使用不同的参数(如Void) 我曾尝试编写super.onPostExecute,但Android Studio不知道我的意思 以及类似问题的不同程序代码 我的代码: public class MainActivity ext
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();
}
}
public类MainActivity扩展了AppCompatActivity{
编辑文本下载文本;
ProgressBar ProgressBar;
文本查看进度文本;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
downloadText=findviewbyd(R.id.downloadLink);
progressBar=findViewById(R.id.progressBar);
设置最大进度条(100);
progressBar.SetUndeterminate(假);
progressText=findViewById(R.id.progressText);
if(shouldAskPermissions()){
askPermissions();
}
}
公共无效开始下载(视图){
DownloadFile DownloadFile=新的DownloadFile();
downloadFile.execute(downloadText.getText().toString());
}
公共无效setProgressText(整数百分比){
setText(“下载bei”+百分比+“%!”);
}
受保护的布尔值shouldAskPermissions(){//Quelle:https://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android
返回(Build.VERSION.SDK\u INT>Build.VERSION\u code.LOLLIPOP\u MR1);
}
@塔吉塔皮(23)
受保护的无效askPermissions(){//Quelle:https://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android
字符串[]权限={
“安卓.权限.读取外部存储”,
“android.permission.WRITE\u外部存储”
};
int requestCode=200;
请求权限(权限、请求代码);
}
静态类下载文件扩展异步任务{//Quelle:https://www.androidhive.info/2012/04/android-downloading-file-by-showing-progress-bar/
@凌驾
公共字符串doInBackground(字符串…sUrl){
试一试{
URL=新URL(sUrl[0]);
URLConnection=url.openConnection();
connection.connect();
int fileLength=connection.getContentLength();
InputStream输入=新的BufferedInputStream(url.openStream());
OutputStream output=新文件OutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_下载).getAbsolutePath()+“/”+“Test.jpg”);
字节数据[]=新字节[8192];
长总计=0;
整数计数;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
出版进度((整数)(总计*100/文件长度));
输出.写入(数据,0,计数);
}
System.out.println(“下载beendet!”);
output.flush();
output.close();
input.close();
}捕获(例外e){
System.out.println(e.getMessage());
}
返回null;
}
}
@凌驾
受保护的void onProgressUpdate(整数…进度){
progressBar.setProgress(progress[0]);
setProgressText(进度[0]);
}
@凌驾
受保护的void onPostExecute(字符串结果){
Toast.makeText(这是“下载beendet!”,Toast.LENGTH\u LONG.show();
}
}
提前感谢。您只需在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
}
静态类下载文件扩展异步任务{//Quelle:https://www.androidhive.info/2012/04/android-downloading-file-by-showing-progress-bar/
@凌驾
公共字符串doInBackground(字符串…sUrl){
试一试{
URL=新URL(sUrl[0]);
URLConnection=url.openConnection();
connection.connect();
int fileLength=connection.getContentLength();
InputStream输入=新的BufferedInputStream(url.openStream());
OutputStream output=新文件OutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_下载).getAbsolutePath()+“/”+“Test.jpg”);
字节数据[]=新字节[8192];
长总计=0;
整数计数;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
出版进度((整数)(总计*100/文件长度));
输出.写入(数据,0,计数);
}
System.out.println(“下载beendet!”);
output.flush();
output.close();
input.close();
}捕获(例外e){
System.out.println(e.getMessage());
}
返回null;
}
//----对此发表评论
// }
@凌驾
受保护的void onProgressUpdate(整数…进度){
progressBar.setProgress(progress[0]);
setProgressText(进度[0]);
}
@凌驾
受保护的void onPostExecute(字符串结果){
Toast.makeText(这个“下载beendet!”,Toas