android 上下文空指针异常
我对 android Context 有一个小问题,我不知道如何解决这个问题。这是我正在使用的代码:
public class TestActivity {
Context context;
public static String getPackageVersion(){
try {
PackageManager pm = context.getPackageManager();
PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
version = packageInfo.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return version;
}
public static boolean checkClientApiVer(String clientApiVer){
int s = RPCCommunicator.strVerToIntVer(clientApiVer);
int c = RPCCommunicator.strVerToIntVer(getPackageVersion());
return (c>=s);
}
public boolean execute() {
serverApiVer = jsonObj.getString("server_api_ver");
Log.w("SERVER API VER","SHOW SERVER API VERSION : "+serverApiVer);
checkClientApiVer(serverApiVer);
}
}
它在这一行中显示 Nullpointer 异常:
PackageManager pm = context.getPackageManager();
实际上我不能使用 this.getPackageManager ()
或 TestActivity.getPackageManager()
并且我无法将 context
设置为 this
。
有什么建议吗?
I have a little issue with android Context and I don't know how to solve the problem.Here is the code I am using :
public class TestActivity {
Context context;
public static String getPackageVersion(){
try {
PackageManager pm = context.getPackageManager();
PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
version = packageInfo.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return version;
}
public static boolean checkClientApiVer(String clientApiVer){
int s = RPCCommunicator.strVerToIntVer(clientApiVer);
int c = RPCCommunicator.strVerToIntVer(getPackageVersion());
return (c>=s);
}
public boolean execute() {
serverApiVer = jsonObj.getString("server_api_ver");
Log.w("SERVER API VER","SHOW SERVER API VERSION : "+serverApiVer);
checkClientApiVer(serverApiVer);
}
}
and it says Nullpointer exception in this line :
PackageManager pm = context.getPackageManager();
Actually I can't use this.getPackageManager()
, or TestActivity.getPackageManager()
and I can't set context
to this
.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
@Roflcoptr 指出了基本的,但实际上,您的类不扩展活动,因此它不是上下文,请将其更改为:
如果您希望它是一个实际的活动,或者,如果它应该只是一个辅助类,实例化时将 Activity 传递给它。
@Roflcoptr pointed out the basic, but actually, your class doesn't extend activity, so it is not a context, change it to:
if you want it to be an actual activity, or, if it should be only a helper class, pass it the activity when it is instantiated.
如果你的课程是一项活动,最好将其用作上下文。如果您需要另一个类中的上下文,则可以在 applicationContext 上有一个单例指针。
在你的清单文件中:
现在你总是可以有一个上下文
If your class is an activity, it's better to use this as a context. If you need a context in another class, you can have a singleton pointer on your applicationContext.
and in your manifest file :
Now you can always have a context with
您应该通过添加不是 Activity 的类的构造函数来初始化上下文,
并在 Activity 中通过发送
this
作为参数来实例化TestActivity
,如下所示:第二种解决方案是扩展 Activity ,您应该覆盖方法
onCreate()
you should to initialize your context by adding a constructor of your class which is not an Activity ,
and in your Activity , instantiate the
TestActivity
by sendingthis
as a parameter like this :The second solution is by extending an Activity , and you should override the method
onCreate()
问题是您声明了上下文,但从未实例化或分配对其的引用。所以它指向null。
通常,您的 TestActivity 应该是 Activity 类或类似类的子类。
在这种情况下你可以做类似的事情
The problem is that you declared context but never instantiated or assigned a reference to it. So it points to null.
Normally you TestActivity should be a subclass from the Activity class or something similar.
In this case you could do something like
您没有创建特定类的任何对象,而不是在类中创建上下文对象并保留活动或应用程序的引用,这会导致 android bcz 中的内存泄漏。
您可以通过执行以下步骤来实现此目的
1>创建应用程序上下文的单例类并在manifest.xml中定义应用程序类;因此它们将是整个应用程序生命周期中的一个上下文对象
2>当您使用该方法时传递上下文。
instead of creating context object in class and keeping reference of activity or application lead to memory leaks in android bcz you are not creating any object of particular class.
you can achieve this by doing below steps
1> create singleton class of application context and define application class in manifest.xml ; so their will be one context object in entire application life cycle
2> pass context when ever you are using that method.