1

我的应用程序似乎在进入新活动时崩溃了。现在这个活动只有一个编辑文本字段,我正在尝试测试我的共享偏好技能。我还没有制作共享首选项文件,但我的印象是它是在访问它时自动制作的。这是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();

    }

}
4

1 回答 1

2

问题在这里:

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)));
于 2012-09-18T02:39:16.773 回答