所以我想在我的项目中获得一个字符串资源但是当我打电话时context.getResources().getString(...),我得到一个NullPointerException.在调试模式下,我发现上下文不是null但是看着它的成员,我发现它mResources是null.为什么没有为活动上下文加载资源?
编辑
显然,这是触发异常的原因
public class MyActivity extends Activity {
SomeClass someClass = new SomeClass(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
public class SomeClass {
private final Context mContext;
public SomeClass(Context context) {
mContext = context;
mContext.getResources().getString(R.string.app_name);
}
}
Run Code Online (Sandbox Code Playgroud)
我不得不按照CommonsWare的建议将初始化someClass移到后面.谢谢.super.onCreate()
我使用我的Android应用程序的应用程序Context作为我的应用程序的"当前状态"信息的存储区域.
我发现在现场,有些情况下,这些信息会在某些人的设备上消失,从而导致各种NullPointerExceptions,因为我希望当应用程序恢复并开始重建必要的活动时数据就在那里.
这通常发生在用户点击"Home",执行其他操作,然后最终回到应用程序中时 - 它会尝试返回到之前的状态,但应用程序Context已经神秘地丢失了以前保存的所有状态信息(在我的情况,几个整数和几个字符串).
我知道这是一个非常模糊的问题,但有没有任何情况(除了用户使用"后退"完全退出应用程序),即使应用程序未终止,应用程序上下文也会完全被破坏?
有没有更好的方法来维护持久的状态信息?
我已经按照本教程在我的Android应用程序中使用SQLite数据库.由于我是初学者,我在理解示例中使用的"context"参数时遇到了问题.
我想调用适配器并插入/更新/删除不扩展活动的类中的记录,在此示例中代表上下文.
现在我不知道在适配器中传递什么作为上下文,因为我没有从活动中调用适配器.
有人可以解释一下吗?
我想知道什么时候Context.bindService()回来false?
我已经尝试进行onBind()返回null,但是true在bindService调用时它仍然会返回并且onServiceConnected不会执行.我也在Google网上论坛上看到了此消息,但没有回复
https://groups.google.com/forum/#!topic/android-developers/ZLl56Mz1jYg
我也找不到bindService的实现,因为它在Context.java中是抽象的,并且搜索"public boolean bindService"也不会产生任何有用的结果(最接近的是ApplicationContext,它似乎不存在于当前的API级别中).
我有一个关于片段中上下文用法的问题.我的问题是我总是得到一个NullpointerException.这是我做的:
创建一个扩展SherlockFragment的类.在那个类中,我有另一个Helper类的实例:
public class Fragment extends SherlockFragment {
private Helper helper = new Helper(this.getActivity());
// More code ...
}
Run Code Online (Sandbox Code Playgroud)
以下是其他Helper类的摘录:
public class Helper {
public Helper(Context context) {
this.context = context;
}
// More code ...
}
Run Code Online (Sandbox Code Playgroud)
每次我调用context.someMethod(例如context.getResources())我都会得到一个NullPointerException.这是为什么?
根据我的理解,Android中的应用程序是一个单例(如果我错了,请纠正我),我们总是只有一个应用程序上下文实例.
那么,从这个角度来看,在我的Application类中保存应用程序Context是不好的做法吗?它会导致大量内存泄漏吗?
这是一个例子:
public class MyApp extends Application {
private static Context appContext = null; // <-- here is the thing!
@Override
public void onCreate() {
appContext = this;
}
public static Context getApplicationContextSingleton () {
return MyApp.appContext;
}
}
Run Code Online (Sandbox Code Playgroud)
这样做的原因是全局访问的类,如PreferencesManager,大多数静态方法总是需要上下文.因此,我不是每次都传递它(甚至将它存储在一个可能很糟糕的实例中),而是考虑存储应用程序上下文.有什么缺点我没看到?
我要做的事情如下:
private static SharedPreferences sharedPreferencesInstance;
public static SharedPreferences getSharedPreferences(final Context context){
if (context==null)
return sharedPreferencesInstance;
if (sharedPreferencesInstance == null)
sharedPreferencesInstance = context.getApplicationContext().getSharedPreferences("prefs", Context.MODE_PRIVATE);
return sharedPreferencesInstance;
}
private static SharedPreferences.Editor sharedPreferencesEditorInstance;
public static SharedPreferences.Editor getSharedPreferencesEditor(final Context context){
if (context==null)
return sharedPreferencesEditorInstance;
if (sharedPreferencesEditorInstance == null)
sharedPreferencesEditorInstance = context.getApplicationContext().getSharedPreferences("prefs", Context.MODE_PRIVATE).edit();
return sharedPreferencesEditorInstance;
}
Run Code Online (Sandbox Code Playgroud)
但在Context泄漏的意义上它是否安全?
我有一个Application课我在其中覆盖getApplicationContext().
我查看了Android源代码,我无法找到它所在的位置?
继承层次结构是:
Application -> ContextWrapper -> Context
public abstract Context getApplicationContext();
Run Code Online (Sandbox Code Playgroud)
getApplicationContext()是Context类中的一个抽象方法,它在类中被重写ContextWrapper.
@Override
public Context getApplicationContext() {
return mBase.getApplicationContext();
}
Run Code Online (Sandbox Code Playgroud)
mBase是Context在ContextWrapper的构造函数中初始化的类型的对象的引用,但是根据代码,Application的类构造函数将null传递给super()ie的构造函数的构造函数ContextWrapper.
public Application() {
super(null);
}
Run Code Online (Sandbox Code Playgroud)
现在传递上下文的唯一可能方法是:
protected void attachBaseContext(Context base) {
if (mBase != null) {
throw new IllegalStateException("Base context already set");
}
mBase = base;
}
Run Code Online (Sandbox Code Playgroud)
但也没有对这种方法的要求.请帮助我找到那个getApplicationContext()叫做的地方吗?
注意:请不要发布任何链接到这些问题,因为我已经完成了每个问题,但仍然没有具体的答案.
我java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
运行我的应用程序时总是得到这个错误.我看到了其他答案,但我不明白如何使用这些答案,我理解上下文存在一些问题,因为没有Activity调用,所以我也尝试传递Activity上下文但没有成功...这是我的代码:
编辑 我已经做了你的答案,它没有用,所以我删除了这个因为我认为这不是解决方案.由于你的答案,我更新了代码(使用上下文作为parmerter到Distance类>到MySingleton类......)
public class MainActivity extends AppCompatActivity {
private Button b;
private TextView t;
private LocationManager locationManager;
private LocationListener listener;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (TextView) findViewById(R.id.textView);
b = (Button) findViewById(R.id.button);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.d("Co", "onLocationChanged");
Distance distance=new Distance();
t.setText("\n " + location.getLongitude() + " " + …Run Code Online (Sandbox Code Playgroud) 我已经看到这种特殊的技术用于启动活动,在我看来,由于静态上下文,我觉得这是一个坏主意,但我希望有人可能有这种方法背后的合理理由.
要启动的活动实现静态启动(Context context)方法,该方法设置intent,flags等,最后启动活动.
public static void launch(Context context){
Intent i = new Intent(context, SomeOtherActivity.class);
// flag stuff
context.startActivity(i);
}
Run Code Online (Sandbox Code Playgroud)
然后,DifferentActivity可以用一行启动SomeOtherActivity.
SomeOtherActivity.launch(DifferentActivity.this);
Run Code Online (Sandbox Code Playgroud)
我喜欢它如何允许你在活动中设置标志,而不是启动它的DifferentActivity,但它似乎不足以合理地将该活动的上下文传递给静态方法.
这不会导致DifferentActivity不被垃圾收集,因为现在静态方法有引用吗?这似乎是一个内存泄漏给我,可能不是一个好主意,只是为了能够保持正在创建的活动中包含的标志.
我在这里缺少哪些东西使这成为一个好习惯吗?