这是我的代码中的一个片段,我在加载时在我的 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