2012-09-18 150 views
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(); 

    } 

} 

回答

2

問題就在這裏:

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

您將文本10int),不"10"String )。 int值被解釋爲資源ID(來自錯誤日誌的0xA,十進制爲10),因此它試圖找到匹配ID 10的資源 - 該資源不存在。

請嘗試以下之一:

​​
+0

所以你說,集文本的方法只能設置字符串? – mango

+0

字符串和資源ID。例如,如果你在['strings.xml'](http://developer.android.com/guide/topics/resources/string-resource.html)中有''string name ='foo'> Some Text',那麼你可以做'view.setText(R.string.foo)'。否則,是的,它必須是一個字符串(或['CharSequence']的其他衍生物(http://developer.android.com/reference/java/lang/CharSequence.html))。 – Eric

+0

謝謝,只是測試了你的解決方案,它的工作。我知道我的問題應該有一些簡單的解決方案。我只是不得不問這個問題。 :我 – mango