1

I am trying to add items to a spinner but I get the error "Unfortunately my app has stopped working". I have created a string array in strings.xml in which I have some items. When the user selects the add item from the MainActivity an editText with a button appears and I want whatever is in the editText to be passed into the spinner. The spinner is in the activity_main.xml and I am working on addactivity.xml. How am I going to pass the new items from the EditText (in addactivity.xml and AddActivity.java) to the spinner in the MainActivity.java and activity_main.xml?

Here is my code:

    public class AddActivity extends Activity{

Spinner spnr1;
EditText edtxAddActivity;
ArrayAdapter<String> spinnerAdapter;
Button btnAddActivity;
String[] actArray;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addactivity);
    addActivityButton();

}

public void addActivityButton() {
    // TODO Auto-generated method stub
    edtxAddActivity = (EditText) findViewById(R.id.etAddActivity);
    spnr1 = (Spinner) findViewById(R.id.spinner1);  
    actArray = getResources().getStringArray(R.array.activities_array);
    spinnerAdapter  = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,actArray);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spnr1.setAdapter(spinnerAdapter);
    btnAddActivity= (Button) findViewById(R.id.btAddActivity);

    btnAddActivity.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub  

            String newactivity = "" + edtxAddActivity.getText().toString();
            spinnerAdapter.add(newactivity);

            spinnerAdapter.notifyDataSetChanged();
        }       
    });
}

}

This is my strings.xml file:

<?xml version="1.0" encoding="utf-8"?>

<string name="app_name">MyAdroidApp</string>
<string name="activity_prompt">Choose an activity</string>

<string-array name="activities_array">
    <item>5-a-side</item>
    <item>Football</item>
    <item>Basketball</item>
    <item>Table Tennis</item>
    <item>Add</item>
</string-array>

The errors displayed are the following.

07-24 14:08:43.397: E/AndroidRuntime(24798): FATAL EXCEPTION: main
07-24 14:08:43.397: E/AndroidRuntime(24798): Process: com.example.myAndroidApp, PID: 24798
07-24 14:08:43.397: E/AndroidRuntime(24798): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myAndroidApp/com.example.myAndroidApp.AddActivity}: java.lang.NullPointerException
07-24 14:08:43.397: E/AndroidRuntime(24798):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
07-24 14:08:43.397: E/AndroidRuntime(24798):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-24 14:08:43.397: E/AndroidRuntime(24798):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-24 14:08:43.397: E/AndroidRuntime(24798):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-24 14:08:43.397: E/AndroidRuntime(24798):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-24 14:08:43.397: E/AndroidRuntime(24798):    at android.os.Looper.loop(Looper.java:136)
07-24 14:08:43.397: E/AndroidRuntime(24798):    at android.app.ActivityThread.main(ActivityThread.java:5001)
07-24 14:08:43.397: E/AndroidRuntime(24798):    at java.lang.reflect.Method.invokeNative(Native Method)
07-24 14:08:43.397: E/AndroidRuntime(24798):    at java.lang.reflect.Method.invoke(Method.java:515)
07-24 14:08:43.397: E/AndroidRuntime(24798):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-24 14:08:43.397: E/AndroidRuntime(24798):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-24 14:08:43.397: E/AndroidRuntime(24798):    at dalvik.system.NativeStart.main(Native Method)
07-24 14:08:43.397: E/AndroidRuntime(24798): Caused by: java.lang.NullPointerException
07-24 14:08:43.397: E/AndroidRuntime(24798):    at com.example.myAndroidApp.AddActivity.addActivityButton(AddActivity.java:46)
07-24 14:08:43.397: E/AndroidRuntime(24798):    at com.example.socialactivities.AddActivity.onCreate(AddActivity.java:33)
07-24 14:08:43.397: E/AndroidRuntime(24798):    at android.app.Activity.performCreate(Activity.java:5231)
07-24 14:08:43.397: E/AndroidRuntime(24798):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-24 14:08:43.397: E/AndroidRuntime(24798):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-24 14:08:43.397: E/AndroidRuntime(24798):    ... 11 more

The problem is that the spinnerAdapter produce a nullpointerexception because the listview is probably empty or not initialised. How can I initialise my array? My string-array is in array.xml in values.

arisAbCy
  • 71
  • 7
  • Is line 56 this one: String newactivity = "" + edtxAddActivity.getText().toString();? Maybe you're trying to set the adapter twice, place spnr1.setAdapter(spinnerAdapter) below spnr1 = findViewById(R.id.spinner1) – Sherekan Jul 24 '14 at 10:33
  • If I do that my app cannot even go to the page AddActivity. It says "Unfortunately....". – arisAbCy Jul 24 '14 at 10:45
  • @user3561214 add your this spnr1.setAdapter(spinnerAdapter); just before Button Listner and in onclick use only String newactivity = "" + edtxAddActivity.getText().toString(); spinnerAdapter.add(newactivity); spinnerAdapter.notifyDataSetChanged(); – Irshad Khan Jul 24 '14 at 10:54
  • I have updated the error log. Now on line 60 is spnr1.setAdapter(spinnerAdapter); – arisAbCy Jul 24 '14 at 10:58
  • I keep on thinking that you're adding the adapter twice, try what @IrshadKhan told you, it should work – Sherekan Jul 24 '14 at 11:02
  • I have done what you have said but I can't go from the MainActivity.java to the AddActivity.java. The AddActivity page does not appear. If I move spnr1.setAdapter(spinnerAdapter); above String newactivity = "" + edtxAddActivity.getText().toString();. I can get to AddActivity. – arisAbCy Jul 24 '14 at 11:04
  • @Sherekan no i am saying that in onclick just notify adapter after adding new string item – Irshad Khan Jul 24 '14 at 11:30
  • The AddActivity.java class does not contain a spinner. It is the MainActivity.java which does. In the addactivity.xml there is only an EditText and a button. When I press the button I want whatever is in the EditText to go into the spinner in activity_main.xml and return to MainActivity.java – arisAbCy Jul 25 '14 at 06:45
  • I have made changes to the post. – arisAbCy Jul 28 '14 at 06:42

2 Answers2

0

NullPointerException at com.example.myAndroidApp.AddActivity.addActivityButton(AddActivity.java:46)

Means you are referecing and using something that is null at that moment. By the looks of it maybe it is one of your view references. My suggestion is put some debug checkpoints one each line of code and execute the application and follow step by step figuring out what is null

nunoh123
  • 1,087
  • 1
  • 10
  • 17
  • This message appears in debug mode: Your content must have a ListView whose id attribute is 'android.R.id.list' – arisAbCy Jul 28 '14 at 06:15
  • Firts of all no one mentioned listview, we are talking about spinners. Second you dont "need to have listview with id = android.R.id.list. android.R.id.list is an android id used so that you can hava a android.R.id.list and android.R.id.empty and they communicate automatically wihthou setting setEmptyView(). As long as the id chosen in xml is the same one chosen in java the reference to the object will stay intact – nunoh123 Jul 28 '14 at 10:19
0

Take a look at R.layout.addactivity layout, you miss something , especially I think findViewById(R.id.etAddActivity); is null, means you don't have it in layout or smtg is wrong in it's namie, id or so

  • The AddActivity.java class does not contain a spinner. It is the MainActivity.java which does. In the addactivity.xml there is only an EditText and a button. When I press the button I want whatever is in the EditText to go into the spinner in activity_main.xml and return to MainActivity.java. How am I going to reference the spinner of the other xml file? – arisAbCy Jul 25 '14 at 06:51
  • Spinner spinner = (Spinner)findViewById(R.id.mySpinner); ArrayAdapter spinnerAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, android.R.id.text1); spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(spinnerAdapter); spinnerAdapter.add("value"); spinnerAdapter.notifyDataSetChanged(); This works for me, so start in debug mode and put breakpoint at spinner initialization, you miss smtg, spinner or editText is null, maybe you have a typo in id of spinner or so..... – Stanojevic Dalibor Jul 25 '14 at 07:53
  • This message appears in debug mode: Your content must have a ListView whose id attribute is 'android.R.id.list' – arisAbCy Jul 25 '14 at 09:50
  • If your activity extends ListActivity change it to Activity, or take a look at http://stackoverflow.com/questions/11050817/your-content-must-have-a-listview-whose-id-attribute-is-android-r-id-list http://stackoverflow.com/questions/16080466/your-content-must-have-a-listview-whose-id-attribute-is-android-r-id-list – Stanojevic Dalibor Jul 28 '14 at 07:31