-2

Of course I know that a Non-Static Method needs to be called from a Non-Static Context.

And am I missing something when I think that

public void methodName(int i) { ... }

is Non-Static ?

Because Android Studio 2.3.2 flaged it as a Static Context so I can't call the following statement from the Method

enter image description here

Method:

public void deleteCard(int id){
    for(int i = 0; i < cards.size(); i++){
        if(cards.get(i).id == id){
            cards.remove(i);
            notifyItemRemoved(i);
            notifyItemRangeChanged(i, cards.size());
        }
    }
}
Noah Mühl
  • 137
  • 2
  • 9

1 Answers1

1

The error message during compilation and the message of stack trace during an exception occuring during the life span of an application provides enough clue of the issue.

In your case it clearly says "Non Static method deleteCard(int) cannot be referenced from a static context". Here you are correct that deleteCard is non static and same is evident from the first part of the message : "Non static method deleteCard(int).. " , but you have missed the second part of the message which tells about the issue i.e. "...can not be referenced from a static context".

You should in such situation see as how you are referencing the field or method. You are using a classname to invoke the method. It is a static context.

All the member instance fields and methods needs an instance of their class and then only using the reference to that object you can access them. Non static member fields and methods belong to an object. You access non static fields of an object and invoke non static methods on an object and for doing so you need a reference of the corresponding class.

You need an object of the class CardAdapter to invoke any instance member method as the method is non static.

nits.kk
  • 5,204
  • 4
  • 33
  • 55
  • Yes, the only thing that I did not understand was the definition of a Static Context. I thought that every Method without the Keyword 'static' is Non-Static. – Noah Mühl May 27 '17 at 15:48