java 如何将上下文从 MainActivity 传递到 Android 中的另一个类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42036278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:18:23  来源:igfitidea点击:

How to pass context from MainActivity to another class in Android?

javaandroidclassmain-activity

提问by akash palaniappan

I am experiencing an issue when trying to pass the context from the MainActivity class to another. Here is the ContextPasser Class.

我在尝试将上下文从 MainActivity 类传递到另一个类时遇到问题。这是 ContextPasser 类。

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 line = new Line(new ContextPasser().context);

in another class called the GameRunner like so:

在另一个名为 GameRunner 的类中,如下所示:

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.

所以,我想创建一个方法来传递 Context 而不接受 Context 类本身的参数。

Please let me know any suggestions or answers. I would really appreciate it. Thanks a lot for your time and consideration!

请让我知道任何建议或答案。我真的很感激。非常感谢您的时间和考虑!

回答by OBX

If your other class is a non-activity class this is how you pass the Context:

如果您的其他课程是非活动课程,您可以通过以下方式传递Context

public class MyNonActivityClass{

// variable to hold context
private Context context;

//save the context received via constructor in a local variable

public MyNonActivityClass(Context context){
    this.context=context;
}

}

And this is how you pass the context :

这就是您传递上下文的方式:

MyNonActivityClass mClass = new MyNonActivityClass(this);

回答by Kevin Krumwiede

OBX's answershows one way to do it. But you should understand why your approach does not work.

OBX 的回答显示了一种方法。但是你应该明白为什么你的方法不起作用。

Instances of Android components such as Applicationand Activityare not fully realized as Contexts until they've been initialized by the framework. This happens sometime before onCreate(...)is called. If you create a newinstance yourself, it won't be initialized and will neither have nor be usable as a Context. For the same reason, calling getApplicationContext()in a field assignment or constructor will return nullor throw an exception.

Android 组件的实例(例如Application和 )在被框架初始化之前Activity不会完全实现为Contexts。这发生在onCreate(...)调用之前的某个时间。如果您new自己创建一个实例,它将不会被初始化,并且既不会也不会用作Context. 出于同样的原因,getApplicationContext()在字段赋值或构造函数中调用将返回null或抛出异常。

MyActivity extends Activity {
  private Context appContext = getApplicationContext(); // wrong

  public MyActivity {
    appContext = getApplicationContext(); // wrong
  }

  public void onCreate(final Bundle savedInstanceState) {
    appContext = getApplicationContext(); // correct
  }
}