java.lang.SecurityException:MODE_WORLD_READABLE

时间:2017-07-20 12:31:18

标签: android listview sharedpreferences android-gridview

我在我的应用程序中没有使用此标志MODE_WORLD_READABLE,但仍然有这个例外吗?这个exception还有其他原因吗,或者这是我做错了什么?

这是我在listviewgridview之间切换的逻辑,我为它们设置了int标志,并在共享首选项中保存/检索它们:

private int currentViewMode = 0 ;
static final int VIEW_MODE_LISTVIEW = 0;
static final int VIEW_MODE_GRIDVIEW = 1;
boolean isGridView;

on onCreate:

currentViewMode = sharedPreferences.getInt("currentViewMode",VIEW_MODE_LISTVIEW);

我的swichView方法:

    private void switchView() {
    SharedPreferences sharedPreferences = 
                        activity.getSharedPreferences("localGridCount",currentViewMode);
    SharedPreferences.Editor editor = sharedPreferences.edit();

    if (VIEW_MODE_LISTVIEW == currentViewMode){
        // ListView
        RecyclerView.LayoutManager mLayoutManager = new 
                       GridLayoutManager(activity, 1, GridLayoutManager.VERTICAL, false);
        gridCount = 1;
        editor.putInt("localGridValue", gridCount);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);
    }else{
        // GridView
        RecyclerView.LayoutManager mLayoutManager = new 
                       GridLayoutManager(activity, 2, GridLayoutManager.VERTICAL, false);
        gridCount = 2;
        editor.putInt("localGridValue", gridCount);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);
    }
    editor.apply();
}

这是我得到的完全例外(Just in Nougat): Long_Exception_Link

1 个答案:

答案 0 :(得分:2)

在您的代码中看起来您使用了 VIEW_MODE_GRIDVIEW ,即常量1等于常量值 MODE_WORLD_READABLE(1),因此您将获得该异常。

因此,使用不同的常量值

参考:https://developer.android.com/reference/android/content/Context.html#MODE_WORLD_READABLE