如何从Main extends Activity调用公共变量

时间:2012-01-15 05:38:43

标签: android android-intent

我是Android Java的新手。我按照教程Sprite游戏进行了一些修改,但是当我尝试从GameView从Main获取变量时,我遇到了问题。实际上,我想从GameView中调用Main中的getsetting()。它在开发屏幕上看起来没问题,但是当我运行它时,我得到了错误。

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; 
   }    

}

1 个答案:

答案 0 :(得分:0)

我认为您应该扩展具有应用程序范围的Application类。如果将所有应用程序范围变量添加到此类,则可以在Application上下文中随时访问它们。

实施例::

public class Appdelegate extends Application{

public boolean soundsetting; 
//OTHER STUFFS   
}

现在可以访问,你可以致电

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

别忘了在ManifestFile中引用这个应用程序....

修改

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

</application>

希望这可以帮到你