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

时间:2012-09-18 02:34:40

标签: android android-edittext 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();

    }

}

1 个答案:

答案 0 :(得分:2)

问题在于:

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

您要将文字设置为10int),而不是"10"String)。 int值被解释为资源ID(0xA,来自您的错误日志,十进制为10),因此它正在尝试查找与ID 10匹配的资源 - 该资源不存在。

请尝试以下方法之一:

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