2013-08-22 84 views
0

我想共享主活動和我的列表片段之間的字符串。所以我想我可以使用Shared.Preferences。由於我剛剛接觸android,剛開始使用共享首選項,所以我面臨這個問題。ListFragments中的共享首選項

這是我的共享優先級

public class AppPrefs { 
    private static final String USER_PREFS = "USER_PREFS"; 
    private SharedPreferences appSharedPrefs; 
    private SharedPreferences.Editor prefsEditor; 
    private String responseXml = "response_xml_prefs"; 
    public AppPrefs(Context context){ 
     this.appSharedPrefs = context.getSharedPreferences(USER_PREFS, Activity.MODE_PRIVATE); 
     this.prefsEditor = appSharedPrefs.edit(); 
     } 
    public String getResponse_xml() { 
     return appSharedPrefs.getString(responseXml, "unknown"); 
     } 
    public void setResponse_xml(String _responser_xml) { 
     prefsEditor.putString(responseXml, _responser_xml).commit(); 
     } 

    } 

這在我mainActivity

Context context = getApplicationContext(); 
    AppPrefs appPrefs = new AppPrefs(context); 
    appPrefs.setResponse_xml(responseXml); 

,這在我的ListFragment

Context context = this.getActivity(); 
    AppPrefs appPrefs = new AppPrefs(context); 
    String responseXml = appPrefs.getResponse_xml(); 

但是當我做Log.e("responseXml", responseXml);

m得到默認字符串「未知」,這是我在共享喜好類中輸入的。 請讓我知道我在哪裏出了錯:(

更新1

public class TabActivity extends Activity { 
MyTask myNewTask; 
String responseXml; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Intent in = getIntent(); 
    String requestXml=in.getStringExtra("xml"); 
    myNewTask = new MyTask(requestXml); 
    myNewTask.setOnResultListener(asynResult); 
    myNewTask.execute(); 


    ActionBar actionBar = getActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    actionBar.setDisplayShowTitleEnabled(true); 

    /** Creating All Tab */ 
    Tab tab = actionBar.newTab() 
      .setText("All") 
      .setTabListener(new CustomTabListener<AllMsgFragment>(this, "All", AllMsgFragment.class,responseXml)); 
    //.setIcon(R.drawable.android); 

    actionBar.addTab(tab); 


    /** Creating Success Tab */ 
    tab = actionBar.newTab() 
      .setText("Success") 
      .setTabListener(new CustomTabListener<SuccessMsgFragment>(this, "Success", SuccessMsgFragment.class,responseXml)); 
    //.setIcon(R.drawable.apple); 

我唐諾如何通過捆綁

} 

OnAsyncResult asynResult = new OnAsyncResult() { 

    @Override 
    public void onResultSuccess(final int resultCode, final String responseXml) { 

     runOnUiThread(new Runnable() { 
      public void run() { 
       storeData(responseXml); 
      } 


     }); 
    } 


    @Override 
    public void onResultFail(final int resultCode, final String errorMessage) { 
     runOnUiThread(new Runnable() { 
      public void run() { 

      } 
     }); 

    } 



}; 

void storeData(String responseXml){ 
    //this.responseXml=responseXml; 
    /*Context context = getApplicationContext(); 
    AppPrefs appPrefs = new AppPrefs(context); 
    appPrefs.setResponse_xml(responseXml);*/ 

    Fragment fragment = new Fragment(); 
    Bundle bundle = new Bundle(); 
    bundle.putString("responseXml", responseXml); 
    fragment.setArguments(bundle); 

} 

}

其顯示空指針異常

編輯2

使用一個靜態變量,解決了我的問題

回答

1

我會建議使用bundle字符串傳遞到您的片段:

Fragment fragment = new Fragment(); 
Bundle bundle = new Bundle(); 
bundle.putString(key, value); 
fragment.setArguments(bundle); 

和片段內(即onCreate())

Bundle bundle = this.getArguments(); 
String myString = bundle.getString(key, defaultValue); 
+1

'SharedPreferences'存儲爲單例。只要它在你的應用程序的範圍內,任何'Context'都是有效的。你使用'Bundle'將數據傳遞給Fragment的建議是正確的。但是,我不明白爲什麼OP的方法不起作用。 – Vikram

+0

我已經更新了代碼。我嘗試使用Bundle。並在我的ListFragment,(onCreateView)我有doneBundle束= this.getArguments(); String myString = bundle.getString(「responseXml」,「unknown」);不是得到一個例外:nullpointer – user1743673

+0

謝謝user2558882,我編輯了答案。 – malimo