-7

This is my first post at StackExchange. I am a very beginner Android developer. I was making an app for saving arrays(lists). But unfortunately when I run my app it shows {Unfortunately, Shop has stopped!}. I am pretty much familiar with this error and many times I have also been able to get through it. But this time, I can't figure out what am I doing wrong. My code looks perfect but somehow keeps showing that error. I am pasting my java code below. Please go through my code and point my mistake! PLEASE~!

AddItem.java

package com.rcube.shop;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AddItem extends Activity{
SharedPreferences store = PreferenceManager.getDefaultSharedPreferences(getApplication());
SharedPreferences.Editor edit = store.edit();
ArrayList<String> itemarray = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_item);
    Button addnewitem = (Button) findViewById(R.id.addnewitem);
    final EditText getitemname = (EditText)findViewById(R.id.getitem);

    addnewitem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String newitemname = getitemname.getText().toString();
            if(newitemname.length()!=0){
                itemarray.add(newitemname);
                Set<String> itemset = new HashSet<String>(itemarray);
                edit.putStringSet("items_set", itemset);
                edit.commit();
            }else{
                Toast.makeText(AddItem.this, "Write Item name before submitting!", Toast.LENGTH_LONG).show();               
            }
        }
    });


}


}

Please tell me what am I doing wrong. And I have add new android activity in Android Manifest also, so this error is not because of that. Thanks again! PEACE~!

Rish
  • 35
  • 4
  • I think your SharedPreferences and Editor are assigned too early. – Phantômaxx Jun 30 '14 at 09:38
  • Put your phone in debug mode. When it crashes, it'll point you the line that's causing the problem. Make sure the app is being debugged at the time(Has a little green bug next to it in the "Devices" list) – Gentatsu Jun 30 '14 at 09:38
  • http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – laalto Jun 30 '14 at 09:47

2 Answers2

4

Seems like you are getting NPE at getApplicationContect()

Put this in onCreate after setContentView

SharedPreferences store = PreferenceManager.getDefaultSharedPreferences(getApplication());
SharedPreferences.Editor edit = store.edit();

You won't get context before that.

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • Thanks Man...! It worked ...Really appreciate it! But can you explain me what I was doing wrong?? and why it didn't worked at first! – Rish Jun 30 '14 at 09:43
  • @user3789667 `setContentView` renders your xml, and you can get application context afterwards. You were trying to refer application context prior to `setContentView`. So it was throwing Null Pointer Exception. Refer more [here](http://developer.android.com/reference/android/app/Activity.html#setContentView%28android.view.View%29) :) – MysticMagicϡ Jun 30 '14 at 09:46
  • @user3789667 mark the answer as correct if issue is solved :) So it gets removed from unanswered list – MysticMagicϡ Jun 30 '14 at 10:13
  • One more thing...every time I click add new button it resets my array list! I want to keep older strings in array but whenever I add new item it resets my array list and then it only shows new items! This error is in this code only, I have reviewed other codes. – Rish Jul 02 '14 at 16:09
  • Will have to check.would do tomorrow from offive. On mobile right now. – MysticMagicϡ Jul 02 '14 at 16:26
0

Put these 2 lines inside onCreate() after findViewById, then you can use context.

SharedPreferences store = PreferenceManager.getDefaultSharedPreferences(getApplication());
SharedPreferences.Editor edit = store.edit();
Aniruddha
  • 4,477
  • 2
  • 21
  • 39