[英]How to retrieve a context from a non-activity class?
I have found one answer that appears to say I should create a separate class and make a static MyApplication object and make a get method. 我找到了一个答案似乎说我应该创建一个单独的类并创建一个静态MyApplication对象并创建一个get方法。 Then any class can call MyApplication.get() to retrieve the context.
然后任何类都可以调用MyApplication.get()来检索上下文。
Is there any other cleaner way? 还有其他清洁方式吗? This is my situation:
这是我的情况:
I have a class A and a class B. Class A contains an object from class B (let's call the object b). 我有一个A类和一个B类.A类包含一个B类对象(让我们调用对象b)。 In class AI call, "b.play()".
在课程AI调用中,“b.play()”。 However, I get a null pointer exception because class B needs to pass a context to the MediaPlayer.create() method.
但是,我得到一个空指针异常,因为类B需要将一个上下文传递给MediaPlayer.create()方法。
Until now I threw together a hack and from class AI called.... "b.play(this)" and simply passed the context to B. However that is pretty ugly and looks like a bad use of OOP. 到现在为止,我把一个黑客和来自AI的类叫起来......“b.play(this)”并简单地将上下文传递给B.然而,这非常丑陋,看起来像是对OOP的错误使用。
Any thoughts? 有什么想法吗?
This problem seem to arise a lot in Android development. 这个问题似乎在Android开发中出现了很多。 One solution to obtaining a reference to a specific Context is subclassing the Application and grab a reference to the Context which you want.
获取对特定Context的引用的一种解决方案是继承Application并获取对所需Context的引用。
public class MyApplication extends Application {
private Context context;
@Override
public onCreate() {
super.onCreate();
this.context = getApplicationContext() // Grab the Context you want.
}
public static Context getApplicationContext() { return this.context; }
}
This solution however requires that you specify the name of your subclass in your manifest. 但是,此解决方案要求您在清单中指定子类的名称。
<application
android:name=".MyApplication"
</application>
You can then use this anywhere in your application like this in non-activity classes. 然后,您可以在非活动类中的应用程序中的任何位置使用它。
MyApplication.getContext(); // Do something with the context! :)
If class B requires a Context to operate, then I don't see any problem having class A provide that to it (through a parameter on the play method, a parameter in a constructor, etc). 如果B类需要一个Context来操作,那么我没有看到任何问题让A类提供它(通过play方法的参数,构造函数中的参数等)。
I don't think you are doing any poor OOP by providing class B the dependencies that it needs to do it's job. 我不认为你通过为B类提供它需要做的工作所需的依赖性来做任何糟糕的OOP。
I've answered also here . 我也在这里回答。
You can do that using ContextWrapper
, as described here. 您可以使用
ContextWrapper
执行此操作, 如此处所述。
For example: 例如:
public class MyContextWrapper extends ContextWrapper {
public MyContextWrapper(Context base) {
super(base);
}
public void someMethod() {
// MediaPlayer.create(this, ...)
}
}
Passing this
around is a viable way of doing things, especially if this
is the activity that creates the object in need of a Context. 传递
this
方法是一种可行的方法,特别是如果this
是创建需要Context的对象的活动。 Sometimes, I'll put the Context into the constructor (like public MyObject(Context context){this.context = context;}
), so that you don't need to send it every time. 有时候,我会把Context放到构造函数中(比如
public MyObject(Context context){this.context = context;}
),这样你就不需要每次都发送它了。 However, if your object is shared across multiple Activities, you should probably update the context it is looking at with the new Activity, though I haven't tested what happens if you use the old activity. 但是,如果您的对象在多个活动中共享,您可能应该使用新活动更新它正在查看的上下文,但我还没有测试使用旧活动时会发生什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.