retrieving a shared preference
<p>I have two activities A and B.</p>

<p>B : is a setting activity that save all the setting in a shared preference.</p>

<p>A : is the main activity and I retrieve the shared preference that was saved in B activity</p>

<p>the problem is if the user use the application for the first time and launch the A activity its forced close.</p>

<p>I think because there is no shared preference saved yet...</p>

<p>the A activity:</p>

<pre><code>public class DawaaActivity extends ListActivity implements View.OnClickListener {

Button add;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences emportPref = getSharedPreferences("dawaaData",MODE_PRIVATE);
if (emportPref.getInt("exists", 0) == 1){
String dawaaList[] = {emportPref.getString("subject", null)};
setListAdapter(new ArrayAdapter&lt;String&gt;(DawaaActivity.this,
android.R.layout.simple_list_item_1,dawaaList));
}
initialaiz();
}

private void initialaiz() {

add = (Button) findViewById(R.id.button3);
add.setOnClickListener(this);
}

public void onClick(View v) {

Intent data = new Intent(DawaaActivity.this,SettingActivity.class);
startActivity(data);
}
}
</code></pre>

<p>the B activity:</p>

<pre><code>public class SettingActivity extends Activity implements View.OnClickListener {
EditText et;
EditText et1;
Spinner list;
TimePicker startTime;
Button save;
Button cancel;
private SharedPreferences exportPref;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
initialaiz();
}
private void initialaiz() {

et = (EditText) findViewById(R.id.text);
et1 = (EditText) findViewById(R.id.text1);
list = (Spinner) findViewById(R.id.spinner);
startTime = (TimePicker) findViewById(R.id.timePicker);
save = (Button) findViewById(R.id.save);
cancel = (Button) findViewById(R.id.cancel);
save.setOnClickListener(this);
cancel.setOnClickListener(this);
}
public void onClick(View v) {

switch(v.getId()){
case R.id.save:
save();
Intent transferData = new Intent(SettingActivity.this,DawaaActivity.class);
startActivity(transferData);
break;
case R.id.cancel:
finish();
break;
}
}
private void save() {

exportPref = getSharedPreferences("dawaaData",MODE_PRIVATE);
SharedPreferences.Editor editor = exportPref.edit();
editor.putString("subject", et.getText().toString());
editor.putString("dosesC", et1.getText().toString());
editor.putString("doses", list.getSelectedItem().toString());
editor.putInt("hour", startTime.getCurrentHour());
editor.putInt("minute", startTime.getCurrentMinute());
editor.putInt("exists", 1);
editor.commit();
Toast.makeText(SettingActivity.this,"data has been saved" ,Toast.LENGTH_SHORT).show();
}

}
</code></pre>

<p>how I can solve the problem ???</p>

<p><strong>EDIT:</strong></p>

<p>I add the ListView in my layout</p>

<p>and I edit the class like this:</p>

<pre><code>public class DawaaActivity extends Activity implements View.OnClickListener {

ListView myList;
SharedPreferences emportPref = getSharedPreferences("dawaaData",MODE_PRIVATE);
String dawaaList[] = {emportPref.getString("subject", "no data yet")};
myList = (ListView) findViewById(R.id.ListView);
myList.setAdapter(new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_list_item_1, dawaaList));
</code></pre>

<p>and this is the logcat:</p>

<pre><code>08-28 14:56:41.112: E/AndroidRuntime(273):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
08-28 14:56:41.112: E/AndroidRuntime(273):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-28 14:56:41.112: E/AndroidRuntime(273):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-28 14:56:41.112: E/AndroidRuntime(273):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-28 14:56:41.112: E/AndroidRuntime(273):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 14:56:41.112: E/AndroidRuntime(273):  at android.os.Looper.loop(Looper.java:123)
08-28 14:56:41.112: E/AndroidRuntime(273):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-28 14:56:41.112: E/AndroidRuntime(273):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-28 14:56:41.112: E/AndroidRuntime(273):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-28 14:56:41.112: E/AndroidRuntime(273):  at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
08-28 14:56:41.112: E/AndroidRuntime(273):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-28 14:56:41.112: E/AndroidRuntime(273):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.os.Looper.loop(Looper.java:123)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-28 14:59:54.272: E/AndroidRuntime(303):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-28 14:59:54.272: E/AndroidRuntime(303):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
08-28 15:02:26.632: E/AndroidRuntime(331):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
</code></pre>

<p>08-28 15:02:26.632: E/AndroidRuntime(331):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
   08-28 15:02:26.632: E/AndroidRuntime(331):   at android.app.ActivityThread.access$2300(ActivityThread.java:125)
   08-28 15:02:26.632: E/AndroidRuntime(331):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
   08-28 15:02:26.632: E/AndroidRuntime(331):   at android.os.Handler.dispatchMessage(Handler.java:99)
   08-28 15:02:26.632: E/AndroidRuntime(331):   at android.os.Looper.loop(Looper.java:123)
   08-28 15:02:26.632: E/AndroidRuntime(331):   at android.app.ActivityThread.main(ActivityThread.java:4627)
   08-28 15:02:26.632: E/AndroidRuntime(331):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
   08-28 15:02:26.632: E/AndroidRuntime(331):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
   08-28 15:02:26.632: E/AndroidRuntime(331):   at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
   08-28 15:02:26.632: E/AndroidRuntime(331):   at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
   08-28 15:02:26.632: E/AndroidRuntime(331):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)</p>
