次のような2つのクラスがあります。
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)