2015-06-20 77 views
1

當我嘗試從片段中初始化我的SharedPreference init時,我在上下文中不斷收到NullPointerException從片段調用sharedPref時上下文的NullPointerException錯誤

以下是錯誤:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference 
      at c.c.modular.SessionManager.<init>(SessionManager.java:68) 
      at c.c.ProfileCompletedFragment.setupData(ProfileCompletedFragment.java:75) 
      at c.c.ProfileCompletedFragment.passDataToFragment(ProfileCompletedFragment.java:210) 
      at c.c.ProfileActivity.onCreate(ProfileActivity.java:287) 

這裏是初始化:

// init 
    public SessionManager(Context context) { 
     this._context = context; 

      pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE); 
      editor = pref.edit(); 

    } 

這裏是片段中的呼叫:

private void setupData() { 
     //sessions manager 
     session = new SessionManager(getActivity()); 

     //establishing db 
     db = new DatabaseHelper(getActivity()); 

     count = 0; 

    } 

編輯:

什麼我已經意識到了由於片段尚未附加到活動本身,試圖找出如何附加它以便我可以調用活動......這是我在下面編輯中的新嘗試,因此getActivity爲null。 ..同樣的問題...

profileJoinedFrag = new ProfileJoinedFragment(); 
     profileCompletedFrag = new ProfileCompletedFragment(); 
     android.support.v4.app.FragmentManager fm = getSupportFragmentManager(); 

     fm.beginTransaction() 
       .replace(R.id.frame_container, profileCompletedFrag).commit(); 

     //Setting up fragment 
     String[] tabTitles = {"Posts", "Joins"}; 
     adapter = new TabAdapter(fm, profileCompletedFrag, profileJoinedFrag, tabTitles); 

//  // Initialize the ViewPager and set an adapter 
     pager = (ViewPager) findViewById(R.id.profilePager); 
     pager.setAdapter(adapter); 

     // Bind the tabs to the ViewPager 
     tabs = (PagerSlidingTabStrip) findViewById(R.id.profileTabs); 
     tabs.setViewPager(pager); 

     //default place send 
     // 

     pager.setCurrentItem(0); 
     profileCompletedFrag.passDataToFragment(userId); 
+0

上下文是空的檢查,即 –

回答

2

,而不是環境中使用應用程序上下文中使用getActivity().getApplicationContext().getSharedPreferences("name",Context.MODE_PRIVATE);

+0

,這甚至不是一個選項,只有getActivity顯示了... – Lion789

+0

getActivity()。getApplicationContext()使用此 –

+0

getActivity爲null,即問題檢查編輯我張貼在上面 – Lion789

0

下面的代碼用來初始化

SharedPreferences preferences = this.getActivity().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); 

,而不是session = new SessionManager(getActivity());

+0

問題是getActivity爲空,因爲我創建了一個片段,但它可能不會附加到活動,關於如何附加它的任何想法沒有fragmentManager,因爲這些片段不是v4支持lib frags – Lion789

+1

請正確調試並查看在片段中使用SharedPreferences時發生了什麼。 – NilayDani

+0

getActivity爲空檢查編輯我張貼在上面 – Lion789

1

您正在將null轉換爲SessionManager,因爲getActivity()正在返回null。這意味着當您運行setupData()時,您的Fragment未附加到任何Activity

嘗試調用setupData()上時Fragment連接可以肯定,像onCreate()onCreateView()只運行一個Fragment方法。

看看並選擇一種在代碼中有意義的方法。

編輯

我可以看到你正在試圖通過調用Fragment的方法來傳遞參數給Fragment。做正確的方法是將它傳遞一個Bundle,就像這樣:

Fragment f = new MyFragment(); 
Bundle args = new Bundle(); 
args.putInt("userId", userId); 
f.setArguments(args); 

,然後在FragmentonCreate()

Bundle args = getArguments(); 
int userId = args.getInt("userId", 0); 
//... Do whatever with the argument 
setupData(); 

就像我之前說的,如果你打電話給setupData()方法onCreate()裏面的getActivity()調用不會返回null

+0

正是這個問題,試圖找出這個問題......這個問題是想弄清楚如何使片段管理器的意義...這是我試過..看看編輯後的代碼 – Lion789

+0

@ Lion789也許現在編輯更清晰了。 – MadEqua

+0

感謝通過參數傳遞比使用接口來傳遞活動更高效?因爲我發佈的答案是我目前正在如何解決這個問題 – Lion789

0

採取什麼MadEqua提到我已經解決了它這樣的,如果其他人正在尋找一個解決方案之後...

我加了一個溝通從片段的活性和連接方法的片段。

因此一旦片段連接到活動中,呼叫到活動作出啓動ID被傳遞到片段......像這樣:

片段:

//since fragment is loaded before activity 
    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     setupData(); 

     //TODO depending which frag to start on then have it make this call 
     ((ProfileActivity) getActivity()).passDataToActivity(true); 
    } 

活性(實現activityCommunicator接口):

@Override 
    public void passDataToActivity(Boolean someValue) { 
      profileJoinedFrag.passDataToFragment(userId); 
    } 
0

使用SharedPreferences preferences = getContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);

+0

我可以請求您請您在答案中添加更多的上下文。僅有代碼的答案很難理解。如果您可以在帖子中添加更多信息,它可以幫助提問者和未來的讀者。 – RBT

+0

'public String getValueFromSharedPref(String key,String defaultvalue) { sp = getContext()。getSharedPreferences(「preference name」,MODE_PRIVATE); return(sp.getString(key,defaultvalue)); }' –