メイン アクティビティとリスト フラグメントの間で文字列を共有したいと考えていました。だから私はShared.Preferencesを使うことができると思った。私はアンドロイドが初めてで、共有設定を使い始めたばかりなので、この問題に直面しています。
これは私の共有設定クラスです
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();
but when i do Log.e("responseXml", responseXml);
m getting a default string "unknown" which i have soted in the shared preference class. please let me know where have i gone wrong :(
Update 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);
I donno how to pass the bundle
}
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);
}
}
its showing null pointer exception
Edit 2
Using a static variable solved my issue