2013-08-22 86 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