gpt4 book ai didi

android - 使用 WindowManager 自定义布局

转载 作者:行者123 更新时间:2023-12-04 05:01:56 27 4
gpt4 key购买 nike

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

引起: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;

然后为 mContext 赋值在您的 GameView 构造函数中,如下所示:
 public GameView(Context context) {
super(context);
mContext = context;
...
}

您现在可以使用 mContext在您的内部 initVars()功能

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com