我正在尝试使用if语句创建动态布局:例如,如果x = 1,则将内容视图设置为layout.xml。这是我到目前为止所拥有的,但是当它运行应用程序崩溃时。
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
int leagueCount = getIntent().getExtras().getInt("leagueCount");
if (leagueCount == 1){
setContentView(R.layout.introact_v2);
}
if (leagueCount == 2){
setContentView(R.layout.introact);
}
}
04-23 20:53:01.496: E/AndroidRuntime(654): FATAL EXCEPTION: main
04-23 20:53:01.496: E/AndroidRuntime(654): java.lang.RuntimeException: Unable to start activity ComponentInfo{co.uk.fantasticmedia.TheEvoStikLeague/co.uk.fantasticmedia.TheEvoStikLeague.IntroActivity}: java.lang.NullPointerException
04-23 20:53:01.496: E/AndroidRuntime(654): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-23 20:53:01.496: E/AndroidRuntime(654): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-23 20:53:01.496: E/AndroidRuntime(654): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-23 20:53:01.496: E/AndroidRuntime(654): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-23 20:53:01.496: E/AndroidRuntime(654): at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 20:53:01.496: E/AndroidRuntime(654): at android.os.Looper.loop(Looper.java:130)
04-23 20:53:01.496: E/AndroidRuntime(654): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-23 20:53:01.496: E/AndroidRuntime(654): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 20:53:01.496: E/AndroidRuntime(654): at java.lang.reflect.Method.invoke(Method.java:507)
04-23 20:53:01.496: E/AndroidRuntime(654): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-23 20:53:01.496: E/AndroidRuntime(654): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-23 20:53:01.496: E/AndroidRuntime(654): at dalvik.system.NativeStart.main(Native Method)
04-23 20:53:01.496: E/AndroidRuntime(654): Caused by: java.lang.NullPointerException
04-23 20:53:01.496: E/AndroidRuntime(654): at co.uk.fantasticmedia.TheEvoStikLeague.IntroActivity.onCreate(IntroActivity.java:36)
04-23 20:53:01.496: E/AndroidRuntime(654): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-23 20:53:01.496: E/AndroidRuntime(654): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-23 20:53:01.496: E/AndroidRuntime(654): ... 11 more