gpt4 book ai didi

android - 如何在 SharedPreferences 中使用 getStringSet() 方法?

转载 作者:行者123 更新时间:2023-11-29 21:19:45 26 4
gpt4 key购买 nike

我的应用程序中有两个不同的 Activity 。第一个 Activity 包含 3 个 fragment ,其中包含用于将字符串添加到 SharedPreferences 的按钮,第二个 Activity 用于显示添加的所有字符串。因为我添加了多个字符串,所以我发现我必须使用 getStringSet() 方法。我的 fragment 代码中没有任何错误,但由于 ClassCastException,它在 Debug模式下崩溃了。根据堆栈,问题是因为 getStringSet。堆栈消息是:

"SharedPreferencesImpl.getStringSet(String, Set) line: 234")

这是 fragment 中的代码:

public class FirstFragment extends Fragment {

public static final String filename = "imagine_dragons";
SharedPreferences someData;
Button bMer;
String merStr = "string from first fragment";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_first, container,
false);

bMer = (Button) rootView.findViewById(R.id.bMer);
bMer.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

someData = getActivity().getSharedPreferences(filename,0);
SharedPreferences.Editor editor = someData.edit();
Set<String> sharedString = someData.getStringSet("sharedString", new HashSet<String>());
sharedString.add("merStr");
editor.putStringSet("sharedString", sharedString);
editor.commit();
}
});
return rootView;
}
}

这是第二个 Activity 的代码。这是关于 setText 的另一个挑战。由于我们在 SharedPreference 中使用了字符串集,所以我们不能使用 setText 方法来显示字符串集。因此此代码包含错误(已编辑):

public class SecAct extends Activity {
public static final String filename = "imagine_dragons";
SharedPreferences someData;
TextView tvSec;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sec);
tvSec = (TextView) findViewById(R.id.tvSec);
someData = getSharedPreferences(filename, 0);
Set<String> sharedString=someData.getStringSet("sharedString", new HashSet<String>());
if(sharedString != null){

Iterator<String> iterator = sharedString.iterator();

while(iterator.hasNext()){

String id = iterator.next();

int start = id.indexOf("[") + 1;
int end = id.indexOf("]")-1;

String items = String.copyValueOf(id.toCharArray(), start, end);


}
}
}
}

我的 minSDK 值为 14,因此它支持 getStringSet() 方法。

这是 LogCat:

    01-02 16:02:39.727: W/asset(7824): Copying FileAsset 0x57249048 (zip:/data/app/com.example.ikincitaslak-1.apk:/resources.arsc) to buffer size 9272 to make it aligned.
01-02 16:02:39.732: W/ActivityThread(7824): Application com.example.ikincitaslak is waiting for the debugger on port 8100...
01-02 16:02:43.512: E/(7824): file /data/data/com.nvidia.NvCPLSvc/files/driverlist.txt: not found!
01-02 16:02:49.827: W/dalvikvm(7824): threadid=1: thread exiting with uncaught exception (group=0x40f33a08)
01-02 16:02:49.847: E/AndroidRuntime(7824): FATAL EXCEPTION: main
01-02 16:02:49.847: E/AndroidRuntime(7824): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ikincitaslak/com.example.ikincitaslak.Sepet}: java.lang.StringIndexOutOfBoundsException: length=9; regionStart=0; regionLength=-2
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463)
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.app.ActivityThread.access$600(ActivityThread.java:162)
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.os.Looper.loop(Looper.java:158)
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.app.ActivityThread.main(ActivityThread.java:5751)
01-02 16:02:49.847: E/AndroidRuntime(7824): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 16:02:49.847: E/AndroidRuntime(7824): at java.lang.reflect.Method.invoke(Method.java:511)
01-02 16:02:49.847: E/AndroidRuntime(7824): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
01-02 16:02:49.847: E/AndroidRuntime(7824): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
01-02 16:02:49.847: E/AndroidRuntime(7824): at dalvik.system.NativeStart.main(Native Method)
01-02 16:02:49.847: E/AndroidRuntime(7824): Caused by: java.lang.StringIndexOutOfBoundsException: length=9; regionStart=0; regionLength=-2
01-02 16:02:49.847: E/AndroidRuntime(7824): at java.lang.String.failedBoundsCheck(String.java:587)
01-02 16:02:49.847: E/AndroidRuntime(7824): at java.lang.String.<init>(String.java:419)
01-02 16:02:49.847: E/AndroidRuntime(7824): at java.lang.String.copyValueOf(String.java:716)
01-02 16:02:49.847: E/AndroidRuntime(7824): at com.example.ikincitaslak.Sepet.onCreate(Sepet.java:36)
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.app.Activity.performCreate(Activity.java:5165)
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
01-02 16:02:49.847: E/AndroidRuntime(7824): ... 11 more

最佳答案

TextView没有 setText采用 Set<Strings> 的方法作为参数。如果您希望集合中的所有字符串都显示在 TextView 中,您将必须创建一个作为集合的合取的字符串。

关于如何做这样的事情有很多教程:one such method

关于android - 如何在 SharedPreferences 中使用 getStringSet() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20880551/

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