2012-01-15 36 views
1

我是Android Java的新手。我跟着教程Sprite遊戲並修改了一下,但是當我嘗試從GameView中獲取Main的變量時,我遇到了問題。實際上,我想從GameView中調用Main中的getsetting()。它在開發屏幕上看起來不錯,但是當我運行它時,出現錯誤。如何從Main拓展活動調用公共變量

package android.learning.pickthemout; 
import android.app.Activity; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.view.Window; 
import android.media.AudioManager; 
import android.media.MediaPlayer; 
import android.media.SoundPool; 
public class Main extends Activity { 
private SoundPool sounds; 
private int sExplosion; 
private int sExplosion_good; 
public boolean soundsetting;  
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(new GameView(this)); } 
public boolean getsetting(){ 
    SharedPreferences test = getSharedPreferences(MenuActivity.PREFS_NAME,0); 
    boolean soundsetting = test.getBoolean("sound", true);  
    System.out.println ("soundsetting: "+ soundsetting); 
    return soundsetting; 
} 
} 


package android.learning.pickthemout; 
import java.util.ArrayList; 
import java.util.List; 
import android.R.string; 
import android.content.Context; 
import android.content.SharedPreferences; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.media.AudioManager; 
import android.media.MediaPlayer; 
import android.media.SoundPool; 
import android.os.Vibrator; 
import android.view.MotionEvent; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.app.Activity; 
import android.view.View; 
import android.widget.TextView; 
import android.content.res.*; 
public class GameView extends SurfaceView { 
    private GameLoopThread gameLoopThread; 
    private List<Sprite> sprites = new ArrayList<Sprite>(); 
    private List<Sprite> sprites_good = new ArrayList<Sprite>(); 
    private List<TempSprite> temps = new ArrayList<TempSprite>(); 
    private List<WinState> winstates = new ArrayList<WinState>(); 
    private long lastClick; 
    private Bitmap bmpBlood; 
    private Bitmap bmpwinner; 
    private SoundPool sounds; 
    private int sExplosion; 
    private int sExplosion_good; 
    /* 
* State-tracking constants 
*/ 
public static final int STATE_LOSE = 1; 
public static final int STATE_PAUSE = 2; 
public static final int STATE_READY = 3; 
public static final int STATE_RUNNING = 4; 
public static final int STATE_WIN = 5;  
private int mMode; 
private Context context; 
private Vibrator v; 
private int level =1; 
private boolean settingsound; 
protected MenuActivity sa; 
private Main main; 
    public GameView(Context context) { 
     super(context);    
     gameLoopThread = new GameLoopThread(this); 
     getHolder().addCallback(new SurfaceHolder.Callback() { 
       @Override 
       public void surfaceDestroyed(SurfaceHolder holder) { 
         boolean retry = true; 
         gameLoopThread.setRunning(false); 
         while (retry) { 
           try { 
            gameLoopThread.join(); 
            retry = false; 
           } catch (InterruptedException e) {} 
         } 
       } 
       @Override 
       public void surfaceCreated(SurfaceHolder holder) { 
         createSprites(); 
         gameLoopThread.setRunning(true); 
         gameLoopThread.start(); 
       } 
       @Override 
       public void surfaceChanged(SurfaceHolder holder, int format, 
           int width, int height) { 
       } 
     }); 
     bmpBlood = BitmapFactory.decodeResource(getResources(), R.drawable.blood1);    
     sounds = new SoundPool(1, AudioManager.STREAM_MUSIC,0); 
     sExplosion = sounds.load(context, R.raw.ouch, 1); 
     sExplosion_good = sounds.load(context, R.raw.mariohello, 1); 
     v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);    
    } 
private void createSprites() { 
     sprites.add(createSprite(R.drawable.bad1)); 
     sprites.add(createSprite(R.drawable.bad2)); 
     sprites.add(createSprite(R.drawable.bad3)); 
     sprites.add(createSprite(R.drawable.bad4)); 
     sprites.add(createSprite(R.drawable.bad5)); 
     sprites.add(createSprite(R.drawable.bad6)); 
     sprites_good.add(createSprite(R.drawable.good1)); 
     sprites_good.add(createSprite(R.drawable.good2)); 
     sprites_good.add(createSprite(R.drawable.good3)); 
     sprites_good.add(createSprite(R.drawable.good4)); 
     sprites_good.add(createSprite(R.drawable.good5)); 
     sprites_good.add(createSprite(R.drawable.good6)); 
    } 
    private Sprite createSprite(int resouce) { 
     Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce); 
     return new Sprite(this, bmp); 
    }  
    private void winlevel (int lvl) { 
     if (lvl == 1) 
     bmpwinner = BitmapFactory.decodeResource(getResources(), R.drawable.winner1); 
     else if (lvl == 2) 
      bmpwinner = BitmapFactory.decodeResource(getResources(), R.drawable.winner2); 
     else if (lvl == 3) 
      bmpwinner = BitmapFactory.decodeResource(getResources(), R.drawable.winner3); 
     else if (lvl == 4) 
      bmpwinner = BitmapFactory.decodeResource(getResources(), R.drawable.winner4);  
     else if (lvl == 5) 
      bmpwinner = BitmapFactory.decodeResource(getResources(), R.drawable.winner5); 
     else if (lvl == 6) 
      bmpwinner = BitmapFactory.decodeResource(getResources(), R.drawable.winner6); 
     else if (lvl == 7) 
      bmpwinner = BitmapFactory.decodeResource(getResources(), R.drawable.winner7); 
     else if (lvl == 8) 
      bmpwinner = BitmapFactory.decodeResource(getResources(), R.drawable.winner8); 
     else if (lvl == 9) 
      bmpwinner = BitmapFactory.decodeResource(getResources(), R.drawable.winner9); 
     else if (lvl == 10) 
      bmpwinner = BitmapFactory.decodeResource(getResources(), R.drawable.winner10); 
     }  
    private void playSound(int n){ 
     settingsound = new Main().getsetting(); 
    } 
    private void playvibrate() { 
    // if (setting.settingsHolder[2]) 
      v.vibrate(300); 
    } 
    @Override 
    protected void onDraw(Canvas canvas) { 
     canvas.drawColor(Color.BLACK); 
     for (int i = temps.size() - 1; i >= 0; i--) { 
       temps.get(i).onDraw(canvas); 
     }  
     for (int i = winstates.size() - 1; i >= 0; i--) { 
      winstates.get(i).onDraw(canvas); 
     } 
     for (Sprite sprite : sprites) { 
       sprite.onDraw(canvas); 
     } 
     for (Sprite sprite : sprites_good) { 
      sprite.onDraw(canvas); 
     }   
    } 
    @Override 
    public boolean onTouchEvent(MotionEvent event) {   
     if (System.currentTimeMillis() - lastClick > 300) { 
       lastClick = System.currentTimeMillis(); 
       float x = event.getX(); 
       float y = event.getY(); 
       synchronized (getHolder()) { 
         for (int i = sprites.size() - 1; i >= 0; i--) { 
          Sprite sprite = sprites.get(i); 
           if (sprite.isCollition(x, y)) {           sprites.remove(sprite); 
             playSound(0); 
             temps.add(new TempSprite(temps, this, x, y, bmpBlood));           
            break;       
           }         
          }       
         for (int i = sprites_good.size() - 1; i >= 0; i--) { 
          Sprite sprite = sprites_good.get(i);         
          if (sprite.isCollition(x, y)) { 
           playSound(1);   
           playvibrate();                 
           break;          
          } 
        } 
         if (sprites.isEmpty()) { 
          winlevel (level); 
          System.out.println("sprites.isEmpty:" + sprites.isEmpty()); //gameLoopThread.setState(STATE_WIN); //WIN 
          winstates.add(new WinState(winstates, this, x, y, bmpwinner,level)); 
          level = level + 1; 
          createSprites(); 
         } 
       }    
      } return true; 
    }  

}

回答

0

我想你應該擴展Application類具有scope.If你所有的應用範圍變量添加到這個類的應用程序,你可以訪問他們的應用程序上下文的任何時間。

例::

public class Appdelegate extends Application{ 

public boolean soundsetting; 
//OTHER STUFFS 
} 

現在訪問,您可以調用

Appdelegate appDel = ((Appdelegate) getApplicationContext()); 
boolean setting = appDel.soundsetting;//CAN ACCESS LIKE THIS 

不要忘記將在manifest資源配置文件這一應用的參考....

編輯

<application android:icon="@drawable/icon" android:label="@string/app_name" 
    android:name="YOUR_APP_DELEGATE"> 
...................................... 
...................YOUR OTHER TAGS 

</application> 

希望這可以幫到你

+0

我創建了新的類pickthemout它擴展了應用程序。它還包含聲音設置()。我也把它放在Manifest文件中,但是從GameView中我不能從GameView中調用它。以下是我在Manifest 2012-01-15 18:07:25

+0

如何在Manifestfile中對此應用程序進行引用? – 2012-01-21 18:58:51

+0

我已更新我的ansewer – 2012-01-23 06:07:10

相關問題