Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javaandroid-Can';t在CustomView外部和CustomView内部使用OnClick函数_Java_Android_Eclipse_Android Layout_Game Physics - Fatal编程技术网

javaandroid-Can';t在CustomView外部和CustomView内部使用OnClick函数

javaandroid-Can';t在CustomView外部和CustomView内部使用OnClick函数,java,android,eclipse,android-layout,game-physics,Java,Android,Eclipse,Android Layout,Game Physics,我目前正在制作一个游戏,该游戏根据此处的教程展开: 我目前的屏幕布局如下所示: 如您所见,方向键盘和难度单选按钮不在我的自定义视图中 编辑:尝试使用共享引用: 在我的主要活动中,我有单选按钮和方向键盘(尚未创建)的一次点击功能: 我的AppPreferences类: import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import andr

我目前正在制作一个游戏,该游戏根据此处的教程展开:

我目前的屏幕布局如下所示:

如您所见,方向键盘和难度单选按钮不在我的自定义视图中

编辑:尝试使用共享引用:

在我的主要活动中,我有单选按钮和方向键盘(尚未创建)的一次点击功能:

我的AppPreferences类:

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class AppPreferences {
    public static final String KEY_DIFFICULTY = "difficulty";
    private static final String APP_SHARED_PREFS = AppPreferences.class.getSimpleName(); 
    private SharedPreferences _sharedPrefs;
    private Editor _prefsEditor;

    public AppPreferences(Context context) {
        this._sharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
        this._prefsEditor = _sharedPrefs.edit();
    }

    public String getDifficulty() {
        return _sharedPrefs.getString(KEY_DIFFICULTY, "");
    }

    public void saveSmsBody(String text) {
        _prefsEditor.putString(KEY_DIFFICULTY, text);
        _prefsEditor.commit();
    }
}
我的GameView类:

    import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


public class GameView extends SurfaceView {
       private GameLoopThread gameLoopThread;
       private List<Sprite> sprites = new ArrayList<Sprite>();
       private List<tempSprite> temps = new ArrayList<tempSprite>();
       private long lastClick;
       private Bitmap bmpBlood;
       private AppPreferences _appPrefs;

       public GameView(Context context, AttributeSet attributeSet) {

             super(context);
             _appPrefs = new AppPreferences(context);
             final String difficulty = _appPrefs.getDifficulty();
             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(difficulty);    
                        gameLoopThread.setRunning(true);
                        gameLoopThread.start();
                    }

                    @Override
                    public void surfaceChanged(SurfaceHolder holder, int format,
                                  int width, int height) {
                    }
             });
             bmpBlood = BitmapFactory.decodeResource(getResources(), R.drawable.image);
       }

       private void createSprites(final String difficulty) {

           if ("1".equals(difficulty))  // easy
           {
               new Thread(new Runnable() {
                public void run() {
                    for (int z =0; z<20; z++ )
                    try
                        {
                        Thread.sleep(5000); 
                        sprites.add(createSprite(R.drawable.image));
                        z++;

                        }
                    catch(InterruptedException e)
                        {
                        e.printStackTrace();
                        }
                }
            }).start();
           }

           else if ("2".equals(difficulty)) // medium
           {
               new Thread(new Runnable() {
                public void run() {
                    for (int z =0; z<40; z++ )
                    try
                        {
                        Thread.sleep(3000); 
                        sprites.add(createSprite(R.drawable.image));
                        z++;

                        }
                    catch(InterruptedException e)
                        {
                        e.printStackTrace();
                        }
                }
            }).start();
           }

           else if ("3".equals(difficulty)) // hard" +

           {
               new Thread(new Runnable() {
                public void run() {
                    for (int z =0; z<60; z++ )
                    try
                        {
                        Thread.sleep(1000); 
                        sprites.add(createSprite(R.drawable.zombie_sprite1));
                        z++;

                        }
                    catch(InterruptedException e)
                        {
                        e.printStackTrace();
                        }
                }
            }).start();
           } 

           else {
                sprites.add(createSprite(R.drawable.zombie_sprite1));
                sprites.add(createSprite(R.drawable.zombie_sprite2));
                sprites.add(createSprite(R.drawable.zombie_sprite3));
                sprites.add(createSprite(R.drawable.zombie_sprite4));
                sprites.add(createSprite(R.drawable.zombie_sprite1));
                sprites.add(createSprite(R.drawable.zombie_sprite2));
                sprites.add(createSprite(R.drawable.zombie_sprite3));
                sprites.add(createSprite(R.drawable.zombie_sprite4));
                sprites.add(createSprite(R.drawable.zombie_sprite1));
                sprites.add(createSprite(R.drawable.zombie_sprite2));
                sprites.add(createSprite(R.drawable.zombie_sprite3));
                sprites.add(createSprite(R.drawable.zombie_sprite4));
           }
        }


       private Sprite createSprite(int resouce) {
             Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
             return new Sprite(this, bmp);
       }

       @Override
       protected void onDraw(Canvas canvas) {


           Resources res = getResources();
           Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.graveyard_background);


            canvas.drawBitmap(background, 0, 0, null);

             for (int i = temps.size() - 1; i >= 0; i--) {
                    temps.get(i).onDraw(canvas);
             }
             for (Sprite sprite : sprites) {
                    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.isCollision(x, y)) {
                                        sprites.remove(sprite);
                                        temps.add(new tempSprite(temps, this, x, y, bmpBlood));
                                        break;
                                  }
                           }
                    }
             }*/
             return true;
       }          
}
我目前正试图通过单选按钮来改变游戏的难度,但现在点击单选按钮,游戏就崩溃了。

(此问题的代码已被转储)

创建一个类
AppPreferences

public class AppPreferences {
     public static final String KEY_DIFFICULTY = "difficulty";
     private static final String APP_SHARED_PREFS = AppPreferences.class.getSimpleName(); //  Name of the file -.xml
     private SharedPreferences _sharedPrefs;
     private Editor _prefsEditor;

     public AppPreferences(Context context) {
         this._sharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
         this._prefsEditor = _sharedPrefs.edit();
     }

     public String getDifficulty() {
         return _sharedPrefs.getString(KEY_DIFFICULTY, "");
     }

     public void saveDifficulty(String text) {
         _prefsEditor.putString(KEY_DIFFICULTY, text);
         _prefsEditor.commit();
     }
}
在类中定义成员

private AppPreferences _appPrefs;
要在您的
onCreate中获取它,请执行以下操作

public GameView(Context context, AttributeSet attributeSet) {
    super(context);
    _appPrefs = new AppPreferences(getApplicationContext());
    String difficulty = _appPrefs.getDifficulty();
并在您选择时设置它

_appPrefs.saveDifficulty("1"); //or "2" or "3"
(此问题中的代码已转储)

创建一个类
AppPreferences

public class AppPreferences {
     public static final String KEY_DIFFICULTY = "difficulty";
     private static final String APP_SHARED_PREFS = AppPreferences.class.getSimpleName(); //  Name of the file -.xml
     private SharedPreferences _sharedPrefs;
     private Editor _prefsEditor;

     public AppPreferences(Context context) {
         this._sharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
         this._prefsEditor = _sharedPrefs.edit();
     }

     public String getDifficulty() {
         return _sharedPrefs.getString(KEY_DIFFICULTY, "");
     }

     public void saveDifficulty(String text) {
         _prefsEditor.putString(KEY_DIFFICULTY, text);
         _prefsEditor.commit();
     }
}
在类中定义成员

private AppPreferences _appPrefs;
要在您的
onCreate中获取它,请执行以下操作

public GameView(Context context, AttributeSet attributeSet) {
    super(context);
    _appPrefs = new AppPreferences(getApplicationContext());
    String difficulty = _appPrefs.getDifficulty();
并在您选择时设置它

_appPrefs.saveDifficulty("1"); //or "2" or "3"

使用我将调用和修改的简单静态setter和getter方法解决了此问题。

使用我将调用和修改的简单静态setter和getter方法解决了此问题。

使用SharedReferences-我不完全确定在该示例中如何使用它。你能给我一些建议吗?使用SharedReferences——我不完全确定从那个例子中我会如何使用它。你能给我一些建议吗?非常感谢你的快速回复。似乎我还没有得到它-你能快速看一下我的新代码吗?\u appPrefs=new-AppPreferences(getApplicationContext());未被识别-我的新代码是从第一篇文章编辑的。如果您有时间看一看,将不胜感激!将appPrefs定义为类成员-在类内部和onCreate方法外部,就像您在主要活动中定义的一样我相信我已经这样做了,不是吗?更新的代码可以在最初的帖子中看到。非常感谢您的快速回复。似乎我还没有得到它-你能快速看一下我的新代码吗?\u appPrefs=new-AppPreferences(getApplicationContext());未被识别-我的新代码是从第一篇文章编辑的。如果您有时间看一看,将不胜感激!将appPrefs定义为类成员-在类内部和onCreate方法外部,就像您在主要活动中定义的一样我相信我已经这样做了,不是吗?更新的代码可以在最初的帖子中看到。