Problemas para organizar el código en paquetes

On my Android project I have the main Activity llamado TestFIO, which is in the package org.testing.file.io.main, and I tried to keep it clear and sent all the functions I had to a new class called FileManipulator, which is located at org.testing.file.io.main.manipulator. Here is how the FileManipulator la clase se parece a:

package org.testing.file.io.main.manipulator;

// imports here

public class FileManipulator extends TestFIO {
    public String readFileFromCard(String location) {
        // some code here
    }

    // more functions here
}

And here is an example of TestFIO:

// header with package and imports

import org.testing.file.io.main.manipulator.FileManipulator;

public class TestFIO extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final String[] fileString = readFileFromCard(Environment.getExternalStorageDirectory() + "test.txt");
    }
}

The problem is that Eclipse is underlining readFileFromCard and showing the following error:

Eclipse Error Dialog

What am I doing wrong or how is the correct way to organize my code in packages?

PS: Sorry if this is a dumb question, I'm coming from iOS development.

preguntado el 01 de febrero de 12 a las 04:02

You are calling a Child's method in your Parent Class and its a violation of inheritance Rule -

2 Respuestas

The compile problem is because you're trying to call a method defined in a subclass from the superclass. Inheritance doesn't work that way; subclasses inherit all public and protected methods from the superclass, but superclasses don't know anything about the methods of their subclasses.

Additionally, it doesn't seem reasonable to have FileManipulator extiende tu Activity class. Does FileManipulator pass the "is-a" test, in other words, is it a kind of Activity? It seems more like it's a "helper" class that the Activity will use to do its work. In that case, FileManipulator no debería extenderse TestFIO but rather be stand-alone, created by TestFIO.

Respondido 01 Feb 12, 08:02

What should I do for my class to work on the Activity ¿clase? FileManipulator usos getBaseContext() to show up a Toast so I extended the Activity. Sorry if this is a dumb question, I'm coming from iOS development. - Nathan Campos

There are a couple of ways to get a "helper" class to invoke UI changes. The simplest is to just pass the instance of Activity cuando creas el FileManipulator, but there's a caveat to that simple solution: you can only invoke methods that change the UI from the thread that created the UI (sometimes called the "UI Thread"). Depending on how FileManipulator is being used, that might be OK in your app or might cause exceptions. - E-Riz

A more robust but slightly more complex solution would be to make the "helper" class an AnsycTask instead. Here is a good tutorial about AyncTask and some other options: vogella.de/articles/AndroidPerformance/article.html - E-Riz

I see TestFIO is parent class, FileManipulator is child class according to your code. Then you can't call child class' method, you need a instance of FileManipulator. Am I wrong?

Respondido 01 Feb 12, 08:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.