2012-11-18 67 views
0

嘗試達到我的活動並更改一些整數時,我得到nullpointerexception。 這是什麼樣子: MainActivity.java嘗試達到活動時的nullpointerexception

public class MainActivity extends Activity { 

    public static SoundManager mSoundManager = new SoundManager(); 

    private static final String TAG = MainActivity.class.getSimpleName(); 


    private MainGamePanel gamePanel; 
    SharedPreferences myPrefs; 

    public int win = 0; 
    public int fail = 0; 

    int wins = 0; 
    int fails = 0; 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    // requesting to turn the title OFF 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    // making it full screen 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    // set our MainGamePanel as the View 
    setContentView(new MainGamePanel(this)); 

    myPrefs = this.getSharedPreferences("hgbdata", 0); 

     try { 
     wins = myPrefs.getInt("wins", 0); 
     fails = myPrefs.getInt("fails", 0); 
     if ((wins != 0) && (fails != 0)) { 
     gamePanel.winn = wins; 
     gamePanel.failn = fails; 
     } 
     } catch (NullPointerException npe) { 
      Log.d(TAG, "Nothing to load"); 
     } 

    //INIT SOUND 
    mSoundManager.initSounds(getBaseContext()); 
    //SOUNDS 
    mSoundManager.addSound(1, R.raw.draw); 
    mSoundManager.addSound(2, R.raw.cheer); 
    mSoundManager.addSound(3, R.raw.boo); 

    } 

. . . 


public void win() { 
    win++; 
} 

public void fail() { 
    fail++; 
} 



} 

這是我想從到達活動: MainGamePanel.java

public class MainGamePanel extends SurfaceView implements 
SurfaceHolder.Callback { 

private static final String TAG = MainGamePanel.class.getSimpleName(); 

private MainThread thread; 
private MainActivity activity; 

. . . 

public void update() { 

//somewhere inside the update() 
activity.win(); 

. . . 

activity.fail(); 

} 

} 

這是日誌:

11-18 10:23:47.336: D/MainThread(1190): Starting game loop 
11-18 10:23:47.336: D/MainThread.initTimingElements()(1190): Timing elements for stats initialised 
11-18 10:23:47.386: D/gralloc_goldfish(1190): Emulator without GPU emulation detected. 
11-18 10:24:19.925: D/dalvikvm(1190): GC_CONCURRENT freed 356K, 2% free 28540K/28999K, paused 132ms+30ms, total 441ms 
11-18 10:24:20.965: W/dalvikvm(1190): threadid=13: thread exiting with uncaught exception (group=0x40a13300) 
11-18 10:24:20.965: E/AndroidRuntime(1190): FATAL EXCEPTION: Thread-105 
11-18 10:24:20.965: E/AndroidRuntime(1190): java.lang.NullPointerException 
11-18 10:24:20.965: E/AndroidRuntime(1190):  at com.nti.hanga.gubbe.MainGamePanel.update(MainGamePanel.java:591) 
11-18 10:24:20.965: E/AndroidRuntime(1190):  at com.nti.hanga.gubbe.MainThread.run(MainThread.java:92) 
11-18 10:24:21.235: I/AndroidRuntime(1190): VM exiting with result code 0, cleanup skipped. 

MainGamePanel.java:591activity.win();

謝謝你的幫忙。

+0

沒有將MainActivity分配給「活動」參考的代碼 – endryha

回答

1

你甚至不應該嘗試從SurfaceView引用你的活動。

定義一個接口並使用回調讓活動知道遊戲結束。

public Interface GameOverListener { 
    void onGameOver(boolean won); 
} 

在您的自定義面視圖

ArrayList<GameOverListener > listeners = new ArrayList<GameOverListener >(); 

... 

public void setGameOverListener(GameOverListener listener){ 
    listeners.add(listener); 
} 

在你更新事件

for (GameOverListener listener:listeners){ 
    listener.onGameOver(gameWon?true:false); // here you pass true if they won, false if they lost 
} 

在你的活動:

public class MainActivity extends Activity implements GameOverListener { 

... 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    ... 
    gamePanel.setGameOverListener(this); 
    ... 
} 

public void onGameOver(boolean won){ 
    if (won){ 
     // they won! 
    } else { 
     // they lost! 
    } 
} 

您可以通過添加改善表面視圖類removeGameOverLis tener並檢查你是否在setGameOverListener中添加了兩次相同的偵聽器。

+0

謝謝。有用!現在我只需要弄清爲什麼當我重新開始遊戲時我的SharedPreferences沒有被加載。 –

+0

很高興聽到它的幫助。祝您共享SharedPreferences。如果你不明白,就開個新的問題......祝你好運。 – Simon

0

如果任activity.win()activity.fail()是在MainGamePanel.java文件的591線,這可能意味着activity == null。你確定你已經初始化它嗎?

+0

這是初始化GamePanel的活動。該活動應該在啓動時被初始化。或者我錯過了什麼?我要在問題中向你展示我的創造。 –

+0

你永遠不會有'activity = this'這樣的行,所以'activity'將總是'null'。 – nhaarman

相關問題