如何正确使用load数组并保存数组方法?

时间:2013-05-08 20:23:23

标签: java android

我有以下代码,专为用户点击按钮而设计,当他们点击按钮时,他们正在查看的特定字符串被收藏并存储在其他地方。

我有两个问题。

  1. 我现在有什么问题?单击按钮时崩溃。

  2. 您将如何完成加载字符串的方法,以便加载并保存到数组中,以便用户以后可以看到该数组?

  3. 感谢您的时间!!

    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
    

1 个答案:

答案 0 :(得分:1)

loadArray函数返回String[]。您可能希望使用它:

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

稍后您可以在第二部分中询问此数组。希望这可以帮助。

更新 [根据您的评论]

所以,如果你想在TextView中抓取文本并希望将它添加到List / String数组中:

1)尝试从TextView获取String

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

2)使用`loadArray'从SharedPreferences获取现有列表:

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

3)将新字符串添加到其中:

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)再次保存。

saveArray(array, "favorites");