-3

I have a variable in the class "MainActivity" with the name modeNr, it is protected so it should be accessible from within the package, however whenever I try to alter it from a class in the same package it gives the error: "Non-static field 'modeNr' cannot be referenced from a static context". I use the following line to alter the variable:

MainActivity.modeNr = 1;

Any ideas on what the problem is and how to correct it?

NoahKr
  • 1
  • 4

4 Answers4

0

you are trying to change non-static member from static function. you need to make that varible static as well or need to create object of that class. lets suppose,

class Test {
 int node = 0;
 static int node1 =10; 

}

class changeNode {

public static void changeNode(){
 new Test().node = somevalue;
//or you need to make node static and change like this
Test.node1 = some value 

}
}
Muneeb Nasir
  • 2,414
  • 4
  • 31
  • 54
0

Sorry I can't comment.

Your modeNr is not static variable. You need to define it like:

protected static String modeNr="ddddd"

since you modify it by MainActivity.modeNr, in this way the variable must be a static variable.

Surely
  • 1,649
  • 16
  • 23
0

To get rid of that error you need to define your variable as static.

static String modeNr="soemthing"

To access a static variable either access it via static member or via an object.

underdog
  • 4,447
  • 9
  • 44
  • 89
0

Just make modeNr variable static.

And I think the log Non-static field 'modeNr' cannot be referenced from a static context is explaining everything.

RediOne1
  • 10,389
  • 6
  • 25
  • 46