【问题标题】:NullPointerException at getCacheDir() androidgetCacheDir() android 上的 NullPointerException
【发布时间】:2014-06-23 07:20:02
【问题描述】:

我正在尝试以编程方式清除我的应用数据。但我得到了一个NullPointerEXception,代码如下。

这是我的代码:-

public class MyApplication extends Application {
private static MyApplication instance;

@Override
public void onCreate() {
    super.onCreate();
    instance = this;
}

public static MyApplication getInstance(){
    return instance;
}

public void clearApplicationData() {
    File cache = getCacheDir();
    File appDir = new File(cache.getParent());
    if(appDir.exists()){
        String[] children = appDir.list();
        for(String s : children){
            if(!s.equals("lib")){
                deleteDir(new File(appDir, s));
                Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s +" DELETED *******************");
            }
        }
    }
}

public static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }

    return dir.delete();
}

}

当我尝试这样做时,我得到了一个NullPointerException:-

File cache = getCacheDir();

我该如何解决这个问题?

【问题讨论】:

  • 你是如何调用clearApplicationData() 的,对象是如何实例化的?另请发布堆栈跟踪。
  • 我认为您对抛出异常的位置是错误的。请再次检查。也许调试代码并检查每一行。我认为这样的原因是,这条线在这里会引起任何问题是没有意义的。
  • 使用上下文参考调用​​将解决您的问题:)

标签: java android nullpointerexception singleton


【解决方案1】:

试试下面的代码:-

File cache = getApplicationContext().getCacheDir();

出现此错误是因为getCacheDir() 函数需要应用程序上下文。

【讨论】:

  • getCacheDir 在他的情况下应该可以正常工作,因为 Applicaiton 扩展了 Context ,并且您将获得相同的上下文:developer.android.com/reference/android/app/Application.html。事实上,使用任何 Context 类都是完全安全的,而不仅仅是 application-context
【解决方案2】:

使用

   File cache = mContext.getCacheDir();

即重写clearApplicationData 函数并传递活动上下文,因为getCacheDir() 函数需要上下文

public void clearApplicationData(Context mContext) {
    File cache = mContext.getCacheDir();
    File appDir = new File(cache.getParent());
    if(appDir.exists()){
        String[] children = appDir.list();
        for(String s : children){
            if(!s.equals("lib")){
                deleteDir(new File(appDir, s));
                Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s +" DELETED *******************");
            }
        }
    }
}

【讨论】:

  • getCacheDir 在他的情况下应该可以正常工作,因为 Applicaiton 扩展了 Context ,并且您将获得相同的上下文:developer.android.com/reference/android/app/Application.html。事实上,使用任何 Context 类都是完全安全的。
  • @androiddeveloper 感谢通知,用第二个选项编辑了我的答案。
【解决方案3】:

我认为该错误是因为您没有使该类成为要使用的真正的应用程序类。

检查清单,“application”标签的“android:name”属性设置正确以引用此类。

为了检查它是否被初始化,请在“onCreate()”方法中写入日志。

此外,您只能先使用:“MyApplication getInstance()”或使用“((MyApplication )getApplicationContext())”来调用“clearApplicationData()”。

另外,请注意,应用程序上下文的这种“设计模式”适用于 99% 的情况。唯一不起作用的情况是,当您有内容提供者时,因为在这种情况下不会初始化应用程序类。

如果这不是错误的原因,请尝试调试代码并查看它是否真的进入了这一行并在那里崩溃。

【讨论】:

    猜你喜欢
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多