Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用setContentView在oncreate方法中切换布局。这可能吗?_Android_Android Layout - Fatal编程技术网

Android 使用setContentView在oncreate方法中切换布局。这可能吗?

Android 使用setContentView在oncreate方法中切换布局。这可能吗?,android,android-layout,Android,Android Layout,我尝试以类似的方式使用setContentView切换活动的内容: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (condition) { setContentView(R.layout.layout1); } else { setContentView(R.layout.layout2); } ... }

我尝试以类似的方式使用setContentView切换活动的内容:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

if (condition) {

  setContentView(R.layout.layout1);

} else {

  setContentView(R.layout.layout2);
}

...
}
在文件夹中: layout1.xml layout2.xml

它可以工作,但是如果在setConentView之后,我想引用布局中的控件2..像TextView一样,使用FindViewById..我得到一个错误,程序崩溃。
有什么问题吗

如果要在
活动
中引用某个版面中的
视图
,则需要:

  • 确保它存在于给定的布局中
  • 确保使用
    setContentView()
    方法设置给定布局

  • 你能再给我一点源代码吗。您引用活动中的控件以及布局的xml代码的位置?有许多代码..它们不相关..但本质上两个xml文件是相同的..除了第二个xml包含第一个xml的相同控件,并且具有相同的Id+其他Id。请仅在需要的条件下尝试引用您的TextView很高兴你能进来。另外,如果您提供了错误,这将有很大帮助。应用程序意外地出现了问题。请再试一次。就像findViewById在布局中找不到控件一样,会崩溃。所有的控件都有同样的问题。克劳迪奥,你需要从Logcat发布准确的崩溃日志。