2014-10-02 5 views
1

내 sharedprefs 클래스의 이름이 UserData입니다.SharedPreferences를 호출하면 내 응용 프로그램이 onManagedUpdate에서 충돌합니다.

내 HUD에 대한 인터넷 용 호출 할 수 있으며 이렇게

Text levelText = new Text(0, 40, AssetLoaderUtil.mFontMENU, "LEVEL: " 
      + UserData.getInstance().getMaxUnlockedLevel(), 
      mContext.getVertexBufferObjectManager()){ 

     @Override 
     protected void onManagedUpdate(float pSecondsElapsed) { 
      super.onManagedUpdate(pSecondsElapsed); 

      //This call to UserData works \/\/\/ 

      this.setText("LEVEL: " + UserData.getInstance().getMaxUnlockedLevel()); 

     } 

    }; 

처럼 잘 작동하지만, 같은 클래스에서,이 때문에 UserData를에, playerGold에 추가 할 통화의 응용 프로그램을 충돌합니다.

mNPCSprite = new MyAnimatedSprite(310, 35, 
      AssetLoaderUtil.mPlayerTextureRegion, mContext) { 

     @Override 
     protected void onManagedUpdate(float pSecondsElapsed) { 
       super.onManagedUpdate(pSecondsElapsed); 

      if (mPlayerSprite.collidesWith(this)) { 

       mCurrentPlayerHealth -= 1; 


      //This call to UserData to add Gold, Crashes the app upon calling it. 
      // 
      //Leaving this line uncommented will crash app when i touch the NPC. \/\/\/ 

      UserData.getInstance().addPlayerGold(15); 

      //yet, this toast, which gets a value from UserData, works fine \/\/\/ 

       mContext.toastOnUIThread("Welcome to map1, Gold in UserData: " 
         + UserData.getInstance().getPlayerGold(), 
         Toast.LENGTH_LONG); 


       // this.setIgnoreUpdate(true); 
      } 
     } 



    }; 

    mNPCSprite.setZIndex(10); 
    this.attachChild(mNPCSprite); 

/////////////////// UserData를 //////////////////

package WorldScene.test.test; 

import android.content.Context; 
import android.content.SharedPreferences; 

public class UserData { 

private static UserData INSTANCE; 

// Include a 'filename' for our shared preferences 
private static final String PREFS_NAME = "GAME_USERDATA"; 

/* These keys will tell the shared preferences editor which 
    data we're trying to access */ 

private static final String UNLOCKED_LEVEL_KEY = "unlockedLevels"; 

private static final String EXP_KEY = "playerExp"; 


private static final String SOUND_KEY = "soundKey"; 

private static final String PLAYER_GOLD = "playerGold"; 

/* Create our shared preferences object & editor which will 
    be used to save and load data */ 
private SharedPreferences mSettings; 
private SharedPreferences.Editor mEditor; 

// keep track of our max unlocked level 
private int mUnlockedLevels; 

// keep track of our max unlocked level 
private int mPlayerExp; 

// keep track of whether or not sound is enabled 
private boolean mSoundEnabled; 

// keep track of whether or not sound is enabled 
private int mPlayerGold; 

UserData() { 
    // The constructor is of no use to us 
} 

public synchronized static UserData getInstance() { 
    if(INSTANCE == null){ 
     INSTANCE = new UserData(); 
    } 
    return INSTANCE; 
} 

public synchronized void init(Context pContext) { 
    if (mSettings == null) { 
     /* Retrieve our shared preference file, or if it's not yet 
     * created (first application execution) then create it now 
     */ 
     mSettings = pContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 

     /* Define the editor, used to store data to our preference file 
     */ 
     mEditor = mSettings.edit(); 

     /* Retrieve our current unlocked levels. if the UNLOCKED_LEVEL_KEY 
     * does not currently exist in our shared preferences, we'll create 
     * the data to unlock level 1 by default 
     */ 
     mUnlockedLevels = mSettings.getInt(UNLOCKED_LEVEL_KEY, 1); 

     /* Retrieve our current unlocked levels. if the UNLOCKED_LEVEL_KEY 
     * does not currently exist in our shared preferences, we'll create 
     * the data to unlock level 1 by default 
     */ 
     mPlayerGold = mSettings.getInt(PLAYER_GOLD, 0); 


     /* Retrieve our current unlocked levels. if the UNLOCKED_LEVEL_KEY 
     * does not currently exist in our shared preferences, we'll create 
     * the data to unlock level 1 by default 
     */ 
     mPlayerExp = mSettings.getInt(EXP_KEY, 1); 


     /* Same idea as above, except we'll set the sound boolean to true 
     * if the setting does not currently exist 
     */ 
     mSoundEnabled = mSettings.getBoolean(SOUND_KEY, true); 
    } 
} 

/* retrieve the max unlocked level value */ 
public synchronized int getMaxUnlockedLevel() { 
    return mUnlockedLevels; 
} 

/* retrieve the max unlocked level value */ 
public synchronized int getPlayerGold() { 
    return mPlayerGold; 
} 

/* retrieve the max unlocked level value */ 
public synchronized int getPlayerExp() { 
    return mPlayerExp; 
} 


/* retrieve the boolean defining whether sound is muted or not */ 
public synchronized boolean isSoundMuted() { 
    return mSoundEnabled; 
} 

/* This method provides a means to increase the max unlocked level 
* by a value of 1. unlockNextLevel would be called if a player 
* defeats the current maximum unlocked level 
*/ 
public synchronized void unlockNextLevel() { 
    // Increase the max level by 1 
    mUnlockedLevels++; 

    /* Edit our shared preferences unlockedLevels key, setting its 
    * value our new mUnlockedLevels value 
    */ 
    mEditor.putInt(UNLOCKED_LEVEL_KEY, mUnlockedLevels); 

    /* commit() must be called by the editor in order to save 
    * changes made to the shared preference data 
    */ 
    mEditor.commit(); 
} 


/* This method provides a means to increase the max unlocked level 
* by a value of 1. unlockNextLevel would be called if a player 
* defeats the current maximum unlocked level 
*/ 
public synchronized void addPlayerGold(int g) { 
    // Increase the max level by 1 
    mPlayerGold += g; 

    /* Edit our shared preferences unlockedLevels key, setting its 
    * value our new mUnlockedLevels value 
    */ 
    mEditor.putInt(PLAYER_GOLD, mPlayerGold); 

    /* commit() must be called by the editor in order to save 
    * changes made to the shared preference data 
    */ 
    mEditor.commit(); 
} 

public synchronized void addPlayerExp(int x) { 
    // Increase the max level by 1 
    mPlayerExp += x; 

    /* Edit our shared preferences unlockedLevels key, setting its 
    * value our new mUnlockedLevels value 
    */ 
    mEditor.putInt(EXP_KEY, mPlayerExp); 

    /* commit() must be called by the editor in order to save 
    * changes made to the shared preference data 
    */ 
    mEditor.commit(); 
} 



/* The setSoundMuted method uses the same idea for storing new data 
* into the shared preferences. First, we overwrite the mSoundEnabled 
* boolean, use the putBoolean method to store the data, and finally 
* commit the data to the shared preferences 
*/ 
public synchronized void setSoundMuted(final boolean pEnableSound) { 
    mSoundEnabled = pEnableSound; 
    mEditor.putBoolean(SOUND_KEY, mSoundEnabled); 
    mEditor.commit(); 
} 

} 미국에서

//////////// 로그 캣 //////////

10-02 15:47:22.212: W/dalvikvm(848): threadid=11: thread exiting with uncaught exception (group=0x40fea2a0) 
    10-02 15:47:22.235: E/AndroidRuntime(848): FATAL EXCEPTION: UpdateThread 
    10-02 15:47:22.235: E/AndroidRuntime(848): java.lang.NullPointerException 
    10-02 15:47:22.235: E/AndroidRuntime(848): at WorldScene.test.test.UserData.addPlayerGold(UserData.java:143) 
    10-02 15:47:22.235: E/AndroidRuntime(848): at Scene.WorldScene$1.onManagedUpdate(WorldScene.java:115) 
    10-02 15:47:22.235: E/AndroidRuntime(848): at org.andengine.entity.Entity.onUpdate(Entity.java:1167) 
    10-02 15:47:22.235: E/AndroidRuntime(848): at org.andengine.entity.Entity.onManagedUpdate(Entity.java:1402) 
    10-02 15:47:22.235: E/AndroidRuntime(848): at org.andengine.entity.scene.Scene.onManagedUpdate(Scene.java:284) 
    10-02 15:47:22.235: E/AndroidRuntime(848): at org.andengine.entity.Entity.onUpdate(Entity.java:1167) 
    10-02 15:47:22.235: E/AndroidRuntime(848): at org.andengine.engine.Engine.onUpdateScene(Engine.java:604) 
    10-02 15:47:22.235: E/AndroidRuntime(848): at org.andengine.engine.Engine.onUpdate(Engine.java:599) 
    10-02 15:47:22.235: E/AndroidRuntime(848): at org.andengine.engine.LimitedFPSEngine.onUpdate(LimitedFPSEngine.java:51) 
    10-02 15:47:22.235: E/AndroidRuntime(848): at org.andengine.engine.Engine.onTickUpdate(Engine.java:561) 
    10-02 15:47:22.235: E/AndroidRuntime(848): at org.andengine.engine.Engine$UpdateThread.run(Engine.java:833) 
    10-02 15:47:22.251: W/ActivityManager(470): Force finishing activity WorldScene.test.test/.WorldActivity 

unlockNextLevel() OnManagedUpdate에서 호출 된 경우 erData가 앱을 종료합니다. UserData에서 변수를 확인하기 위해 호출 할 수 있지만 변수를 변경하지 않는 이유는 무엇입니까?

+1

당신이 UserData를 클래스, 특히 addPlayerGold의 코드를 공유 할 수 있습니까? –

+0

UserData 클래스 추가 –

+0

UserData의 init() 메서드를 어딘가에 호출하고 있습니까? NullPointerException은'mEditor.putInt (PLAYER_GOLD, mPlayerGold); 행에 있습니다. 그렇다면 에디터를 초기화하지 않은 것이 문제입니다. – CaptainTeemo

답변

0

해답은 CaptainTemo입니다. 비슷한 문제를 가진 사람의 경우, 이 UserData를 초기화하지 않았나요

했다 WorldActivity에 UserData.init()를 호출합니다 :

@Override 
protected Scene onCreateScene() { 


    this.getEngine().registerUpdateHandler(new FPSLogger()); 
    // Initializes the Engine 
    mWorldEngine = this.getEngine(); 

    //Start with touch enabled 
    TOUCH_ENABLED = true; 

    mMainCurtain = new Sprite(0, 0, AssetLoaderUtil.FadeTextureRegion, this.getVertexBufferObjectManager()); 

    SceneManager.initialize(this); 
    SceneManager.mLoadingScene.create(); 
    SceneManager.mGameIntroScene.create(); 
    SceneManager.mMainMenuScene.create(); 

    ///////////////////////// 
    //ADDED SHAREDPREFS INIT() METHOD \/\/\/ 
    //////////////////////// 

    UserData.getInstance().init(getApplicationContext()); 

    ... 
    } 
관련 문제