I am experiencing an issue when trying to pass the context from the MainActivity class to another. Here is the ContextPasser Class.
public class ContextPasser extends Application{
public Context context;
public ContextPasser(){
context = getApplicationContext();
}}
But if I use the context variable in the other class, like this:
Line line = new Line(new ContextPasser().context);
I am getting a null pointer exception. I want to get a solution which does not involve passing in a context variable because I have looked at all of the other stackoverflows and they do not seem to help me with that. One of the solutions that I found was:
Context context;
MyHelperClass(Context context){
this.context=context;
}
This does not help me because I am calling the Line class which is,
Line line = new Line(new ContextPasser().context);
in another class called the GameRunner like so:
public class GameRunner extends Activity {
Line line = new Line(new ContextPasser().context);
public void draw(Canvas canvas){
line.draw(canvas);
getWindow().getDecorView().setBackgroundColor(Color.BLACK);
}
public void update(){
line.update();
}
}
So, I want to create a method that will pass the Context without accepting parameters of the Context class themselves.
Please let me know any suggestions or answers. I would really appreciate it. Thanks a lot for your time and consideration!