Android 使用WindowManager自定义布局
出于某种原因,我不断收到一个错误,因为我正在尝试实现 原因:java.lang.NullPointerException 在pap.crowslanding.GameView。(GameView.java:49) 第49行是这个吗Android 使用WindowManager自定义布局,android,Android,出于某种原因,我不断收到一个错误,因为我正在尝试实现 原因:java.lang.NullPointerException 在pap.crowslanding.GameView。(GameView.java:49) 第49行是这个吗 WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); 我这样做是因为我没有
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
我这样做是因为我没有参加任何活动,有什么建议吗
编辑:
然后在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
,从哪里获取ctx
值?这是一个上下文值。。。。您是否正在传递活动的上下文?我们需要看到更多的代码;因为我不知道如何处理ctx,它需要它。我找不到它的任何东西此函数在哪里?您刚才所做的是创建了一个空上下文,这就是为什么它会给您一个空指针。您需要获取活动的上下文并将其传递给此函数调用。我已编辑了我的代码以向您展示我想要做的事情,thanks@RyanAlexander您的公共游戏视图(上下文)
有一个上下文的实例。在类文件mContext
中创建一个局部变量,并在构造函数中赋值mContext=context
。然后,您可以在代码中的任何其他位置使用mContext
。问题是,我不知道需要传递什么上下文,我只想使用显示参数,因此我不确定要在活动上写什么。我编辑了我的页面以显示使用GameView作为上下文的活动。我已经应用了您的编辑,但不幸的是,这仍然为我提供了一个空指针异常,我遇到了问题:04-17 15:47:45.045:E/AndroidRuntime(22639):在pap.crowslanding.GameView.initVars(GameView.java:110)04-17 15:47:45.045:E/AndroidRuntime(22639):在pap.crowslanding.GameView.(GameView.java:67)
public GameView(Context context) {
super(context);
mContext = context;
...
}