Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8499161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:35:41+00:00 2026-06-11T00:35:41+00:00

I have two activities A and B. B : is a setting activity that

  • 0

I have two activities A and B.

B : is a setting activity that save all the setting in a shared preference.

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

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

I think because there is no shared preference saved yet…

the A activity:

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<String>(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);
}
}

the B activity:

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

}

how I can solve the problem ???

EDIT:

I add the ListView in my layout

and I edit the class like this:

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<String>(this,android.R.layout.simple_list_item_1, dawaaList));

and this is the logcat:

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)

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:35:42+00:00Added an answer on June 11, 2026 at 12:35 am

    The problem is probably here:

    String dawaaList[] = {emportPref.getString("subject", null)};
    setListAdapter(new ArrayAdapter<String>(DawaaActivity.this,
    android.R.layout.simple_list_item_1, dawaaList));
    

    If you don’t have any subject you will get a null and then probably a nullpointer somewhere.

    Try to check the null, or initialize it with something if null, like:

    if (dawaaList[] == null) { 
       new String[]{"No subjects"}; 
    }
    

    and please, post your logcat for further help.

    EDIT:

    You can handle it this way:

    just add a ListView in your layout

        <ListView
            android:id="@+id/myListView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            />
    

    and populate it in your Activity

        setContentView(R.layout.main);
        ListView listView = (ListView) findViewById(R.id.myListView);
        SharedPreferences pref = getSharedPreferences("dawaaData", MODE_PRIVATE);
        String items = pref.getString("subject", "item1,item2,item3");
        String[] listItems = items.split(",");
        listView.setAdapter(new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1, listItems));
    

    I’ve choose a comma as a delimiter because SharedPreferences cannot store arrays. You should use a Set and pref.getStringSet (from api 11), or serialize the list (json o anything).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have four activities namely, Demo_tabActivity.java [main activity] Tabhost.java The below two activities are
I have four activities namely, Demo_tabActivity.java [main activity] Tabhost.java The below two activities are
I have two Activities. In my main Activity I am creating an intent to
I have two activities, one is main and I have another activity called Test
I have an Android application that contains two Activities . Activity A has a
I have two activities, the one Main.java that initially loads I have a couple
I have two activities. In first I come to the second activity from first
This is my situation. I have two activities: ONE and TWO. In TWO activity
I have two activities, activity1 is starting activity2. in activity 2 I registered an
I have one problem using ActivityGroup. I have two activities inside an ActivityGroup and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.