我有两个类,例如:
public class Game extends Activity
public class CoreGame extends Game
在CoreGame课程中,我有:
public void init(){
layout = (RelativeLayout)findViewById(R.id.rlayout);
}
CoreGame类行的执行返回NullPointerException
。layout = (RelativeLayout)findViewById(R.id.rlayout);
为什么?
OnCreate :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.game);
core = new CoreGame(this);
core.runCoreGame();
}
Core.runCoreGame():
public void runCoreGame(){
init();
}
public void init(){
layout = (RelativeLayout)findViewById(R.id.rlayout);
}
堆栈跟踪:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{gianbalex.mgs_revenge/gianbalex.mgs_revenge.Game}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1637)
at gianbalex.mgs_revenge.CoreGame.initVariables(CoreGame.java:118)
at gianbalex.mgs_revenge.CoreGame.runCoreGame(CoreGame.java:84)
at gianbalex.mgs_revenge.Game.onCreate(Game.java:28)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)