空指针ObsecureSharedPreferences

时间:2013-11-05 06:40:30

标签: android android-activity sharedpreferences android-fragmentactivity

这是我的代码中的一个片段,我在我的android FragmentActivity中得到一个null_pointer在加载时,我不知道为什么。它发生在最终的SharedPreferences。

public class BaseActivity extends FragmentActivity {

    private static final String MY_PREFS_FILE_NAME = "check_status";
    final SharedPreferences _prefs = new ObscuredSharedPreferences(this, this
            .getApplication().getApplicationContext()
            .getSharedPreferences(MY_PREFS_FILE_NAME, Context.MODE_PRIVATE));
    public static Context _appContext;
    private ListView _baseDrawerList;
    private DrawerLayout _baseDrawer;
    private CustomActionBarDrawerToggle _baseDrawerToggle;
    private String[] _menuItemsControl;// for profile and settings.
    private String[] _menuItemsControlSplit_1;
    private FragmentManager _fManager;
    private LoginFragment _loginFragment;
    private MainDisplayFragment mf;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        _fManager = getSupportFragmentManager();
        mf = new MainDisplayFragment();
        setContentView(R.layout.activity_base_drawer);

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        _baseDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        // set a custom shadow that overlays the main content when the drawer
        // opens
        _baseDrawer.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);

        initlize_menu();

我只是想安全地存储我想要的状态,并认为这是最好的方式。

以下是例外:

11-05 06:37:31.532: E/AndroidRuntime(13674): FATAL EXCEPTION: main
11-05 06:37:31.532: E/AndroidRuntime(13674): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.development/com.development.BaseActivity}: java.lang.NullPointerException
11-05 06:37:31.532: E/AndroidRuntime(13674):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
11-05 06:37:31.532: E/AndroidRuntime(13674):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-05 06:37:31.532: E/AndroidRuntime(13674):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-05 06:37:31.532: E/AndroidRuntime(13674):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-05 06:37:31.532: E/AndroidRuntime(13674):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 06:37:31.532: E/AndroidRuntime(13674):    at android.os.Looper.loop(Looper.java:137)
11-05 06:37:31.532: E/AndroidRuntime(13674):    at android.app.ActivityThread.main(ActivityThread.java:4745)
11-05 06:37:31.532: E/AndroidRuntime(13674):    at java.lang.reflect.Method.invokeNative(Native Method)
11-05 06:37:31.532: E/AndroidRuntime(13674):    at java.lang.reflect.Method.invoke(Method.java:511)
11-05 06:37:31.532: E/AndroidRuntime(13674):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-05 06:37:31.532: E/AndroidRuntime(13674):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-05 06:37:31.532: E/AndroidRuntime(13674):    at dalvik.system.NativeStart.main(Native Method)
11-05 06:37:31.532: E/AndroidRuntime(13674): Caused by: java.lang.NullPointerException
11-05 06:37:31.532: E/AndroidRuntime(13674):    at com.development.BaseActivity.<init>(BaseActivity.java:52)
11-05 06:37:31.532: E/AndroidRuntime(13674):    at java.lang.Class.newInstanceImpl(Native Method)
11-05 06:37:31.532: E/AndroidRuntime(13674):    at java.lang.Class.newInstance(Class.java:1319)
11-05 06:37:31.532: E/AndroidRuntime(13674):    at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
11-05 06:37:31.532: E/AndroidRuntime(13674):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
11-05 06:37:31.532: E/AndroidRuntime(13674):    ... 11 more

1 个答案:

答案 0 :(得分:1)

您可能需要移动,

_prefs = new ObscuredSharedPreferences(this, this
        .getApplication().getApplicationContext()
        .getSharedPreferences(MY_PREFS_FILE_NAME, Context.MODE_PRIVATE));

进入OnCreate,这段代码在BaseActivity的构造函数之前调用,因此当你访问this或this.xxx时可能无法完成BaseActivity的初始化