2

出于某种原因,我不断收到 Error inflating Class 消息,因为我正在尝试实现

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

第49行是这个

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

我这样做是因为我没有参加活动,有什么建议吗?

编辑:

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

    });
}       

}

4

1 回答 1

3

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

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

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()

于 2013-04-17T14:14:38.387 回答