我在尝试将sharedpreferences中的字符串值加载到我的片段上的字符串值时收到空指针异常。
我让sharedpreference工作正常,所以我知道字符串正在保存,但是我不能用我的共享偏好值替换片段中的字符串值。
MainActivity
SharedPreferences saveLocation = getSharedPreferences(SAVE, MODE_PRIVATE);
SharedPreferences.Editor editor = saveLocation.edit();
editor.putString("name", marker.getTitle());
editor.commit(); //applies the name to XML
FlickrFragment
public class FlickrFragment extends Fragment{
GridView mGridView;
ArrayList<GalleryItem> mItems;
ThumbnailDownloader mThumbnailThread;
String textToSearch = "Keating Hall";
基本上“Keating Hall”是用于生成照片网格视图的文本。这是正常的,但是,我正在尝试用共享偏好值替换“Keating Hall”,但在我尝试过的所有方法中都没有成功。
如果我的描述需要更新,请告诉我,以便更好地反映我需要帮助的内容。
谢谢。
已更新:包含LogCat
12-14 15:12:25.161 14902-14902/com.example.ramap E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.ramap, PID: 14902
java.lang.NullPointerException
at com.example.ramap.FlickrFragment.<init>(FlickrFragment.java:41)
at com.example.ramap.adapter.TabsPagerAdapter.getItem(TabsPagerAdapter.java:25)
at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:97)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837)
at android.support.v4.view.ViewPager.populate(ViewPager.java:987)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
at android.view.View.measure(View.java:16660)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5150)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16660)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5150)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
at android.view.View.measure(View.java:16660)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5150)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2421)
at android.view.View.measure(View.java:16660)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2058)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1189)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1374)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1076)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5905)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:807)
at android.view.Choreographer.doCallbacks(Choreographer.java:601)
at android.view.Choreographer.doFrame(Choreographer.java:562)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:791)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5633)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
at dalvik.system.NativeStart.main(Native Method)