android - 使用 WindowManager 自定义布局

标签 android

出于某种原因,我不断收到错误膨胀类消息,因为我正在尝试实现

引起:java.lang.NullPointerException 在 pap.crowslanding.GameView.(GameView.java:49)

第49行是这个

WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

我这样做是因为我不在 Activity 中,有什么建议吗?

编辑:

public class GameView extends SurfaceView {
static final long FPS = 10;
public Bitmap bmp;
public SurfaceHolder holder;
public LoopGameThread loopGameThread;
public static Sprite sprite;
public LayoutInflater inflater;

//maze variables
  public int width;                 
  public int height;
  private float cellWidth;
  private boolean[][] north;     // is there a wall to north of cell i, j
  private boolean[][] east;
  private boolean[][] south;
  private boolean[][] west;
  private boolean[][] visited;
  private double size;
  boolean done = false;

  Display display = new 
  Paint paint = new Paint();




//maze variables
   public GameView(Context context) {
    super(context);
   WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
   Display display = wm.getDefaultDisplay();
   Point screenSize = new Point();
    display.getSize(screenSize);
    int width = screenSize.x;
    int height = screenSize.y;

}
public GameView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO
}

public GameView(Context context, AttributeSet attrs) {

    super(context, attrs);

    //maze variables
    initVars(attrs);
    initMaze();
    generate(1, 1);
    //maze variables
    loopGameThread = new LoopGameThread(this);
    holder = getHolder();

    holder.addCallback(new Callback() {

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {

        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            loopGameThread.isStart(true);
            loopGameThread.start();
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {

        }

    });

    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.crow);
    sprite = new Sprite(this, bmp);

}


//MAZE VARIABLES

private void initVars(AttributeSet attrs) {
      //requires display variables
  }

然后在我的 initVars 中,我需要使用这些显示变量

编辑2:

package pap.crowslanding;


import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.Button;

public class Game extends MainActivity{
static boolean pressedUp = false;
 protected static GameView gameV;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tester1);


    Button moveLeft = (Button) findViewById(R.id.button1);
    gameV = (GameView)findViewById(R.id.game_view);



    moveLeft.setOnTouchListener(new View.OnTouchListener() {

        private Handler mHandler;

        @Override public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (mHandler != null) return true;
                mHandler = new Handler();
                mHandler.postDelayed(mAction, 100);
                break;
            case MotionEvent.ACTION_UP:
                if (mHandler == null) return true;
                mHandler.removeCallbacks(mAction);
                mHandler = null;
                break;
            }
            return false;
        }

        Runnable mAction = new Runnable() {
            @Override public void run() {
                System.out.println("Performing action...");
                mHandler.postDelayed(this, 100);
                GameView.sprite.movementGo();
            }
        };

    });
}       

}

最佳答案

您需要将上下文传递给调用此函数的任何类。

例如,您可以将本地上下文值保留在您的类中,您可以在函数中引用它。首先,您需要创建一个构造函数,它将上下文传递给您的类以供使用。

public class Example {
    Context mContext;

    public Example(Context mContext;){
        this.mContext = mContext;
    }

    public someFunction(){
        ...
        WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        ...
     }
}

在创建类实例并使用它的 Activity 类中,您需要像这样传入 Activity 的上下文:

public class SomeActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Example test = new Example(this); //You are passing in the Activity as the context here
        test.someFunction();

    }
}

编辑您的编辑:

在 GameView 类中创建一个新的局部变量:

private Context mContext;

然后在 GameView 构造函数中为 mContext 赋值,如下所示:

 public GameView(Context context) {
      super(context);
      mContext = context;
      ...
  }

您现在可以在 initVars() 函数中使用 mContext

关于android - 使用 WindowManager 自定义布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16062225/

相关文章:

具有多线程的Android sqlite

android - 为 android studio 项目生成签名 APK 时,通用包装器中没有匹配的架构

java - 图像未保存在我的相机应用程序中

java - 应用程序在服务启动时崩溃

java - 使用 Intent 强制关闭从主类切换到 ListActivity

android - ffmpeg在android上的播放

java - Android - dlopen 失败 : file offset for the library

android - 如何将android sdk添加回eclipse?

Android Actionbar 向上按钮与系统返回按钮

android - 运动事件 Action 移动在 android studio 模拟器中不起作用