0

我想在主要活动和我的列表片段之间共享一个字符串。所以我想我可以使用 Shared.Preferences。由于我是 android 新手并且刚刚开始使用共享首选项,因此我面临这个问题。

这是我的共享偏好类

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);

我得到一个默认字符串“未知”,我已经在共享首选项类中找到了它。请让我知道我哪里出错了:(

更新 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

使用静态变量解决了我的问题

4

1 回答 1

1

我建议使用捆绑包将字符串传递给您的片段:

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);
于 2013-08-22T18:35:55.597 回答