-1

The problem is in the title, can someone please help me? Other similar problems on stackoverflow doesn't work for me. Android Studio tells me "Method does not override method from superclass.", because of this, i was going through stackflow and tried different solutions, like:

  1. Matching the Paramters String, Integer, String with my Methods
  2. Have tried to use different parameters for the methods(like Void)
  3. Have tried to write super.onPostExecute, but Android Studio doesn't know what i mean with this.
  4. And different programmcodes from similear problems

My code:

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();
    }
}

Thanks in advance.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Christopher
  • 59
  • 10

1 Answers1

2

You just need to remove bracket "}" before onProgressUpdate() and add this "}" at last :

Because of this bracket method (progressUpdate() and postExecute()) not getting include in AsyncTask, they are getting included in Activity class, and Activity.class has not these override methods, so its showing error.

    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        
    }
NehaK
  • 2,639
  • 1
  • 15
  • 31