1
私はAndroid Javaの初心者です。私はチュートリアルのSpriteゲームに従って少し修正しましたが、GameViewからMainから変数を取得しようとすると問題が発生します。実際、私はGameViewからMainのgetsetting()を呼びたいと思っています。開発画面では正常に見えますが、実行するとエラーが発生します。メイン変数からpublic変数を呼び出すにはどうすればいいですか?アクティビティ
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;
}
}
私はアプリケーションを拡張する新しいクラスのpickthemoutを作成しました。 soundsetting()も含まれています。私もManifestファイルにpickthemoutを入れましたが、GameViewからGameViewから呼び出すことはできません。ここで私はマニフェスト android:label = "@ string/app_name"> intent-filter> –
マニフェストファイルでこのアプリケーションのリファレンスを作成するにはどうすればよいですか? –
私は私のansewerを更新しました –