0

我的 android 程序中有 2 种不同的布局,有时我想在它们之间切换。但起初我的程序正在终止并且不起作用,因为未定义布局 2 对象。我怎样才能解决这个问题?

        // d and z are obejcts of another layout

    Button mainButton1 = (Button) findViewById(R.id.d);
    final TextView myTextView1 = (TextView) findViewById(R.id.z);

    mainButton1.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            myTextView1.setText("Bye!");
        }
    });
4

2 回答 2

0

在您的活动中使用它们之前,为所有视图设置 ID。

于 2013-10-04T11:50:22.417 回答
0

您不能这样做,因为在使用 xml 文件的元素之前,我们必须提供对该 xml 文件的引用,否则它将通过空指针异常。如果您必须将一些信息传递给其他活动,您可以将其与您用来启动其他活动的意图一起附加希望它有所帮助

于 2013-10-04T12:04:01.700 回答