0

I have the following code which is designed for the user to click on a button and when they click on the button, the particular string they are viewing is Favorited and stored somewhere else.

I have two questions.

  1. One what's wrong with what I have right now? as it crashes when you click the button.

  2. How would you go about finishing up on the load array method for the string to be loaded and saved to an array so the user could see that array later?

Thanks for your time!!

fav.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            loadArray("favorites");
            favorites = Arrays.copyOf(favorites, favorites.length+1);
            favorites[favorites.length]=display.getText().toString();
            saveArray(favorites, "favorites");

        }
    });

    home.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Intent openStartingPoint = new Intent("starting.rt.Menu2");
            startActivity(openStartingPoint);
        }
    });

    search.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Intent openStartingPoint = new Intent("starting.rt.Search");
            startActivity(openStartingPoint);
        }
    });

    moreapps.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Intent goToMarket;
            goToMarket = new Intent(Intent.ACTION_VIEW, Uri
                    .parse("market://search?q=pub:\"Wompa\""));
            startActivity(goToMarket);
        }
    });

}

public String[] loadArray(String arrayName) {  
    SharedPreferences prefs = getSharedPreferences("preferencename", 0);  
    int size = prefs.getInt(arrayName + "_size", 0);  
    String array[] = new String[size];  
    for(int i=0;i<size;i++)  
        array[i] = prefs.getString(arrayName + "_" + i, null);  
    return array;  
}  

public boolean saveArray(String[] array, String arrayName) {   
    SharedPreferences prefs = getSharedPreferences("preferencename", 0);  
    SharedPreferences.Editor editor = prefs.edit();  
    editor.putInt(arrayName +"_size", array.length);  
    for(int i=0;i<array.length;i++)  
        editor.putString(arrayName + "_" + i, array[i]);  
    return editor.commit();  
} 

Log Cat

05-08 23:10:51.329: I/Process(495): Sending signal. PID: 495 SIG: 9
05-08 23:11:02.360: D/dalvikvm(504): GC_EXTERNAL_ALLOC freed 44K, 53% free 2553K/5379K, external 1625K/2137K, paused 221ms
05-08 23:11:08.599: D/dalvikvm(504): GC_EXTERNAL_ALLOC freed 16K, 51% free 2644K/5379K, external 2707K/3308K, paused 174ms
05-08 23:11:12.570: D/AndroidRuntime(504): Shutting down VM
05-08 23:11:12.570: W/dalvikvm(504): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-08 23:11:12.599: E/AndroidRuntime(504): FATAL EXCEPTION: main
05-08 23:11:12.599: E/AndroidRuntime(504): java.lang.NullPointerException
05-08 23:11:12.599: E/AndroidRuntime(504):  at starting.rt.Base$1.onClick(Base.java:52)
05-08 23:11:12.599: E/AndroidRuntime(504):  at android.view.View.performClick(View.java:2485)
05-08 23:11:12.599: E/AndroidRuntime(504):  at android.view.View$PerformClick.run(View.java:9080)
05-08 23:11:12.599: E/AndroidRuntime(504):  at android.os.Handler.handleCallback(Handler.java:587)
05-08 23:11:12.599: E/AndroidRuntime(504):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 23:11:12.599: E/AndroidRuntime(504):  at android.os.Looper.loop(Looper.java:123)
05-08 23:11:12.599: E/AndroidRuntime(504):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-08 23:11:12.599: E/AndroidRuntime(504):  at java.lang.reflect.Method.invokeNative(Native Method)
05-08 23:11:12.599: E/AndroidRuntime(504):  at java.lang.reflect.Method.invoke(Method.java:507)
05-08 23:11:12.599: E/AndroidRuntime(504):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-08 23:11:12.599: E/AndroidRuntime(504):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-08 23:11:12.599: E/AndroidRuntime(504):  at dalvik.system.NativeStart.main(Native Method)
05-08 23:11:17.869: I/Process(504): Sending signal. PID: 504 SIG: 9
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
jacobohunter
  • 363
  • 1
  • 4
  • 16

1 Answers1

1

loadArray function returns String[]. You might want to use it like:

String [] arr = loadArray("favorites");  

This array can be used later as you asked in 2nd part. Hope this helps.

Update [Based on your comment]

So, if you want to grab text in TextView and wish to add it in a List/String array:

1) Try to get the String from TextView

TextView display = (TextView)findViewById(R.id.display);
String toSave = display.getText().toString();

2) Get the existing list from SharedPreferences using `loadArray':

String [] arr = loadArray("favorites");  

3) Add the new String to it:

ArrayList<String> arrList = new ArrayList<String>();

for (int i=0; i<arr.length(); i++) {
     arrList.add(arr[i]); //add old values to arraylist
}
// add new string to it: 
arrList.add(toSave);

//Get back your array
String [] array = arrList.toArray(new String[arrList.size()]); 

4) Save it Again.

saveArray(array, "favorites");
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • I don't think, I understand where would I put that and how does it help? Thanks though. – jacobohunter May 08 '13 at 21:55
  • Since you didn't specify that which button click causes a crash, I was supposing that it crashes when you click on the `fav` button. So I saw that inside `OnClick()` of `fav.setOnClickListener(new View.OnClickListener() ` it might cause a crash because `loadArray` function returns `String[]` and you haven't specified a return parameter. If its not the problem, please do specify which button is causing the error? Also post the LogCat of error. It would be helpful. – Shobhit Puri May 08 '13 at 22:00
  • It's the fav button. Sorry for not specifying. – jacobohunter May 08 '13 at 22:08
  • I guessed that. What are you trying to do when it clicks? – Shobhit Puri May 08 '13 at 22:16
  • Favorite a string of text that is being displayed while user is reading it they can click the favorite button and it will grab the string and store it in a favorite array. – jacobohunter May 08 '13 at 22:55
  • Where and how would I use the arr? oh and I added the logcat. – jacobohunter May 08 '13 at 23:10
  • Se the updated answer based on your comment. Tweak it according to your situation. – Shobhit Puri May 08 '13 at 23:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29624/discussion-between-jacob-brans-and-shobhit-puri) – jacobohunter May 09 '13 at 00:35