2015-11-04 35 views
-1

所以我最近將我的處理製作的遊戲轉移到了Android Studio中。到目前爲止,我遇到了一個saveStrings問題,並從位於我的資產文件夾中的txt文件加載它們。問題是如果我手動寫入該txt文件的值我需要存儲,然後嘗試加載它給了我一個錯誤..類似與savingStrings,當我保存該值並運行草圖,它不甚至在我指定的文件中顯示保存的值... 我嘗試了一切,我可以找到關於這個主題,但沒有太多。 我也嘗試在清單中加入<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 但它沒有工作。 在PDE中,它工作得很好,但在這裏我無法弄清楚我該怎麼做才能使它工作。在Android Studio中處理:無法正確加載和保存字符串

這是類:

class HScore{ 
float x; 
float y; 
float speed; 
int points; 
boolean savePoints = true; 
String[] savedH = {"0"}; 
String[] loadedH; 
HScore(){ 
    x = -width/5; 
    y = height/14; 
    speed = 0.04F; 
    loadedH = loadStrings(dataPath("hscore.txt")); 
    points = parseInt(loadedH[0]); 

} 
public void display(){ 
    textFont(startFont); 
    fill(355); 
    textSize(width/7); 
    text(points,x,y); 
} 
public void move(){ 
    x = lerp(x,width/9,speed*2); 
} 
public void moveBack(){ 
    x = lerp(x,-width/5,speed*3); 
} 
public void update(){ 

    if (savePoints){ 
    if (score.points > points) { 
     points = score.points; 
    } 
    savedH[0] = str(points); 
    saveStrings(dataPath("hscore.txt"),savedH); 
    savePoints = false; 
    } 
} 
} 

,這是我得到的錯誤:

11-04 15:29:05.568 7590-7630/? E/AndroidRuntime: FATAL EXCEPTION: GLThread 18857 11-04 15:29:05.568 7590-7630/? E/AndroidRuntime: Process: processing.test.game_pre_release_, PID: 7590 11-04 15:29:05.568 7590-7630/? E/AndroidRuntime: java.lang.IllegalArgumentException: File /data/data/processing.test.game_pre_release_/files/data/hscore.txt contains a path separator 11-04 15:29:05.568 7590-7630/? E/AndroidRuntime: at android.app.ContextImpl.makeFilename(ContextImpl.java:2865) 11-04 15:29:05.568 7590-7630/? E/AndroidRuntime: at android.app.ContextImpl.openFileInput(ContextImpl.java:1332) 11-04 15:29:05.568 7590-7630/? E/AndroidRuntime: at android.content.ContextWrapper.openFileInput(ContextWrapper.java:190) 11-04 15:29:05.568 7590-7630/? E/AndroidRuntime: at processing.core.PApplet.createInputRaw(Unknown Source) 11-04 15:29:05.568 7590-7630/? E/AndroidRuntime: at processing.core.PApplet.createInput(Unknown Source) 11-04 15:29:05.568 7590-7630/? E/AndroidRuntime: at processing.core.PApplet.loadStrings(Unknown Source) 11-04 15:29:05.568 7590-7630/? E/AndroidRuntime: at processing.test.game_pre_release_.game_pre_release_$HScore.(game_pre_release_.java:712) 11-04 15:29:05.568 7590-7630/? E/AndroidRuntime: at processing.test.game_pre_release_.game_pre_release_.setup(game_pre_release_.java:129) 11-04 15:29:05.568 7590-7630/? E/AndroidRuntime: at processing.core.PApplet.handleDraw(Unknown Source) 11-04 15:29:05.568 7590-7630/? E/AndroidRuntime: at processing.opengl.PGLES$AndroidRenderer.onDrawFrame(Unknown Source) 11-04 15:29:05.568 7590-7630/? E/AndroidRuntime: at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1531) 11-04 15:29:05.568 7590-7630/? E/AndroidRuntime: at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248) 
+0

你得到什麼錯誤?發佈完整的堆棧跟蹤。你可以發佈[MCVE](http://stackoverflow.com/help/mcve)嗎? –

+0

dataPath(「hscore.txt」);給我這個:/data/data/processing.test.game_pre_release_/files/data/hscore.txt –

+0

創建您保存文件的文件夾,因爲它顯然不存在。 – njzk2

回答

0

你應該看看SharedPreferences和複製粘貼到名爲.txt Android的不同字符串值。

這裏是它如何工作的一個簡單的例子:

SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);  
Editor editor = sharedpreferences.edit(); 
editor.putString("key", "yourString"); 
editor.commit(); 

然後把它找回來:

String yourString = sharedPreferences.getString("storedName", "defaultValue"); 
相關問題