gpt4 book ai didi

java - 如何正确使用加载数组和保存数组方法?

转载 作者:行者123 更新时间:2023-12-01 14:36:26 27 4
gpt4 key购买 nike

我有以下代码,专为用户单击按钮而设计,当他们单击按钮时,他们正在查看的特定字符串将被 Collection 并存储在其他位置。

我有两个问题。

  1. 我现在的东西有什么问题吗?因为当您单击按钮时它会崩溃。

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

感谢您的宝贵时间!!

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

日志猫

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

最佳答案

loadArray 函数返回 String[]。您可能想像这样使用它:

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

稍后可以按照您在第二部分中的要求使用该数组。希望这可以帮助。

更新 [根据您的评论]

因此,如果您想在 TextView 中获取文本并将其添加到列表/字符串数组中:

1)尝试从TextView获取字符串

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");

关于java - 如何正确使用加载数组和保存数组方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16449534/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com