在程序中,我在其它的类中使用getFileStreamPath与主activity类区分。传递主activity中的context到其它类,并使用这个context调用它的方法。
main activity class:
LocalStorage lc = new LocalStorage(this);
other class:
public class LocalStorage {
Context ctx;
public LocalStorage (Context c) {
c = ctx;
File lfile = ctx.getFileStreamPath("Activity.log");
....
很显然我遗漏了一些代码,,因为运行LocalStorage中context 的getFileStreamPath类时会抛出NullPointerException异常。请求大牛指点一二。
你的变量
c = ctx;
应该改为:
ctx = c;
你未初始化的Context ctx
分配到之前的Context c
,所以它一直是null值。
c = ctx ???
Context ctx;
public LocalStorage (Context c) {
c = ctx;