• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Abstract override isn't overriding

23tony

Well-Known Member
I'm facing a bit of a quandry: I'm implementing an interface that isn't cooperating.

Background: this is for a basic HTTP connection to download JSON data. I used the guide at https://developer.android.com/training/basics/network-ops/connecting and implemented pretty much exactly what is there. I'm now trying to implement the interface in another activity, but it seems to not be recognizing one of the functions.

Interface is DownloadCallback:
Code:
public interface DownloadCallback<T> {
    interface Progress {
        int ERROR = -1;
        int CONNECT_SUCCESS = 0;
        int GET_INPUT_STREAM_SUCCESS = 1;
        int PROCESS_INPUT_STREAM_IN_PROGRESS = 2;
        int PROCESS_INPUT_STREAM_SUCCESS = 3;
    }

    /**
     * Call from main thread to update the UI
     * @param result
     */
    void updateFromDownload(T result);

    /**
     * Get the device's active network status as a NetworkInfo object
     * @return
     */
    NetworkInfo getActiveNetworkInfo();

    /**
     * Show progress update
     * @param progressCode one of the constants defined above
     * @param percentComplete 0-100
     */
    void onProgressUpdate(int progressCode, int percentComplete);

    /**
     * Called when process has finished regardless of success of download.
     */
    void finishDownloading();
}

And the activity:
Code:
public class DummyActivity extends AppCompatActivity implements DownloadCallback {
    private NetworkFragment networkFragment;
    private boolean downloading = false;
    private TextView mTextView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dummy);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        mTextView2 = findViewById(R.id.textView2);
        networkFragment = NetworkFragment.getInstance(getSupportFragmentManager(), getString(R.string.base_url));
    }

    @Override
    private void updateFromDownload(String result) {
        mTextView2.setText(result);
    }

    private void startDownload() {
        if (!downloading && networkFragment != null) {
            networkFragment.startDownload();
            downloading = true;
        }
    }
}
(I left off the imports. Package declarations were created automatically by Android Studio and have been verified to be the same)

With this code, I'm getting the warning: Class 'DummyActivity' must either be declared abstract or implement abstract method 'updateFromDownload(T)' from 'DownloadCallback' on the class declaration, and Method does not override method from its superclass on the @override declaration.

I've tried declaring the method private, protected, public, or even leaving that off completely, with no success. I've copied/pasted the function name from the interface just to be sure I wasn't misspelling without noticing. No matter what I do I can't get it to recognize that I'm overriding it.

I'm relatively new to Java and really new to Android so I'm sure I'm missing something pretty obvious, but I can't see what it is. Anyone have any ideas?
 
I just tried adding this dummy function
Code:
    @Override
    public void onProgressUpdate(int progressCode, int percentComplete) {
        int x = 1;
    }

And that does NOT give me the "does not override" error. So I'm pretty sure it's something in my function declaration - but I copied that right from the guide, and it worked on the previous class I was originally building the example in.
 
One thing to note is that the Android developer guides are often not great for noobs. As the name suggests, they assume you are already a developer. The code fragments given in that guide you were following don't provide the whole picture, because it assumes that you already know how to implement a generic interface.

Btw there's a more user friendly library for doing HTTP requests, and that is Volley.

https://www.androidtutorialpoint.com/networking/android-volley-tutorial/
 
Last edited by a moderator:
Back
Top Bottom