gpt4 book ai didi

android - 基本保存/检索共享首选项

转载 作者:行者123 更新时间:2023-11-29 21:58:50 24 4
gpt4 key购买 nike

我的应用似乎在进入新 Activity 时崩溃了。现在这个 Activity 只有一个编辑文本字段,我正在尝试测试我的 sharedpreferences 技能。我还没有制作共享首选项文件,但我的印象是它是在访问它时自动制作的。这是 logcat:

09-18 02:29:30.293: W/ResourceType(428): No package identifier when getting value for resource number 0x0000000a
09-18 02:29:30.303: D/AndroidRuntime(428): Shutting down VM
09-18 02:29:30.315: W/dalvikvm(428): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-18 02:29:30.334: E/AndroidRuntime(428): FATAL EXCEPTION: main
09-18 02:29:30.334: E/AndroidRuntime(428): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mangodeveloper.mcathomie/com.mangodeveloper.mcathomie.McatActivityPlayOptions}: android.content.res.Resources$NotFoundException: String resource ID #0xa
09-18 02:29:30.334: E/AndroidRuntime(428): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-18 02:29:30.334: E/AndroidRuntime(428): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-18 02:29:30.334: E/AndroidRuntime(428): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-18 02:29:30.334: E/AndroidRuntime(428): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-18 02:29:30.334: E/AndroidRuntime(428): at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 02:29:30.334: E/AndroidRuntime(428): at android.os.Looper.loop(Looper.java:123)
09-18 02:29:30.334: E/AndroidRuntime(428): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-18 02:29:30.334: E/AndroidRuntime(428): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 02:29:30.334: E/AndroidRuntime(428): at java.lang.reflect.Method.invoke(Method.java:507)
09-18 02:29:30.334: E/AndroidRuntime(428): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-18 02:29:30.334: E/AndroidRuntime(428): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-18 02:29:30.334: E/AndroidRuntime(428): at dalvik.system.NativeStart.main(Native Method)
09-18 02:29:30.334: E/AndroidRuntime(428): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0xa
09-18 02:29:30.334: E/AndroidRuntime(428): at android.content.res.Resources.getText(Resources.java:201)
09-18 02:29:30.334: E/AndroidRuntime(428): at android.widget.TextView.setText(TextView.java:2857)
09-18 02:29:30.334: E/AndroidRuntime(428): at com.mangodeveloper.mcathomie.McatActivityPlayOptions.onCreate(McatActivityPlayOptions.java:23)
09-18 02:29:30.334: E/AndroidRuntime(428): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-18 02:29:30.334: E/AndroidRuntime(428): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-18 02:29:30.334: E/AndroidRuntime(428): ... 11 more

代码如下:

package com.mangodeveloper.mcathomie;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class McatActivityPlayOptions extends Activity {

private EditText QuestionNumberEt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playoptions);

QuestionNumberEt = (EditText) findViewById(R.id.Etnumberofquestions);
SharedPreferences settings = getSharedPreferences("MYPREFS", 0);
QuestionNumberEt.setText(settings.getInt("MAXROUNDSVALUE", 10));

Button startGameButton = (Button) findViewById(R.id.startgamebutton);
startGameButton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Intent PlayIntent = new Intent (McatActivityPlayOptions.this, McatActivityGame.class);
PlayIntent.putExtra("MAXROUNDS", Integer.parseInt(QuestionNumberEt.getText().toString()));
startActivity(PlayIntent);
}
});
}

@Override
protected void onStop() {
super.onStop();
SharedPreferences settings = getSharedPreferences("MYPREFS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("MAXROUNDSVALUE", Integer.parseInt(QuestionNumberEt.getText().toString()));
editor.commit();

}

}

最佳答案

问题在这里:

QuestionNumberEt.setText(settings.getInt("MAXROUNDSVALUE", 10));

您正在将文本设置为 10 (int),而不是 "10" (String)。 int 值被解释为资源 ID(错误日志中的 0xA,十进制为 10),因此它试图找到与 ID 10 匹配的资源——该资源不存在。

尝试以下操作之一:

QuestionNumberEt.setText(settings.getInt("MAXROUNDSVALUE", 10) + "");
// OR
QuestionNumberEt.setText(String.valueOf(settings.getInt("MAXROUNDSVALUE", 10)));

关于android - 基本保存/检索共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12469644/

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