-1

Here is the parent class:

public class void ParentClass () extends Activity {

private ListView _listView;

@Override
protected onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_parent);

    _listView = (ListView) getViewById(R.id.list_view);

    ChildClass cc = new ChildClass();
}

protected void SetScroll() {
    try {
        _listView.setFastScrollEnabled(false);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Here is the child class:

private void ChildClass extends ParentClass () {

    public ChildClass() {
        SetScroll();
    }
}

Forgive me with regards to the syntax as I'm typing this from memory. The problem with these classes is that the SetScroll function called from the CallParent function does not work because it is unable to find the right _listView reference (it becomes null). What should I do to make it work correctly? Bear in mind that this is just an example.

geft
  • 615
  • 1
  • 6
  • 18

1 Answers1

1

But the view is getting null is because you are extending an activity class and before using its view you need to call the onCreate and set the view (if child activity needs new one). But for that you will have to follow the complete flow of the lifecycle. This will help you in extending an activity class. android how to create my own Activity and extend it?

Call parent class activity like this (Just for info). With your code it wont work. You need to correct your code first. See the link I have posted.

super.SetScroll();
Community
  • 1
  • 1
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • Thank you, that explains it. It's not supposed to be an activity though, and if I lose the inheritance I'd have problems accessing the method since I cannot make it static (contains `startActivity()`) and I can't instantiate it. I suppose I need to find a way to notify the activity and then run the method internally. – geft Jan 11 '15 at 02:07