Android:在非 main.xml 文件中找到 findViewById

标签 android android-layout android-button

我有两个布局:main.xml 和 journal.xml 当我单击 main.xml 布局中的按钮时,将打开 journal.xml 布局。

问题是我想创建一个后退按钮,它将重新打开 main.xml 布局。 当我尝试这样做时:

ImageButton buttonHome = (ImageButton)findViewById(R.id.image);

找不到图片,因为她不在我的main.xml中

你能帮帮我吗

谢谢

代码如下:

public class MDPI_1Activity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.journal);
        ImageButton buttonJournal = (ImageButton)findViewById(R.id.imageButton1); //Journal
        ImageButton buttonHome = (ImageButton)findViewById(R.id.image); //Journal


        buttonJournal.setOnClickListener(new Button.OnClickListener() 
        {

            public void onClick(View v) 
            {

                setContentView(R.layout.journal);
            }
        });


        buttonHome.setOnClickListener(new Button.OnClickListener() 
        {

            public void onClick(View v) 
            {

                setContentView(R.layout.main);
            }
        });

    }

}

“imageButton1”在我的 main.xml 文件中并且工作正常,但“图像”在我的第二个名为“journal”的 xml 文件中并且不存在于 R 文件中。

最佳答案

我已经为你编译了一个代码。这段代码在我这边工作。您可以通过以下方式使用 Button 在两种布局之间切换。

public class TestActivity extends Activity implements OnClickListener {
    Button btnGoToJournal = null;
    Button btnGoToMain = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnGoToJournal = (Button) findViewById(R.id.btn_go_to_journal);
        btnGoToJournal.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.btn_go_to_journal:
            setContentView(R.layout.journal);
            btnGoToMain = (Button) findViewById(R.id.btn_go_to_main);
            btnGoToMain.setOnClickListener(this);
            break;
        case R.id.btn_go_to_main:
            setContentView(R.layout.main);
            btnGoToJournal = (Button) findViewById(R.id.btn_go_to_journal);
            btnGoToJournal.setOnClickListener(this);
            break;
        }       
    }
}

主.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_go_to_journal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to journal" />

</LinearLayout>

Journal.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_go_to_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to Main" />

</LinearLayout>

关于Android:在非 main.xml 文件中找到 findViewById,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9066242/

相关文章:

android - 布局宽度和布局高度

Android Espresso ActivityTest 给出 'package android.support.test.rule does not exist'

android - 在 Android 中查看 holder 类

android - 向上滚动 RecyclerView 时缓存了错误的 viewHolder 对象

android - 使用约束布局垂直居中多个 View

Android 按住按钮发送多次点击

android - Kotlin中的构造函数泛型

android - 使用 Android Jetpack Compose 显示接触点?

java - MaterialButton 与 Button 的尺寸差异

android - 不同版本的自定义组件