gpt4 book ai didi

java - Button2 正在做 Button1 应该做的事情

转载 作者:行者123 更新时间:2023-12-01 13:41:45 25 4
gpt4 key购买 nike

我是 Android 编程新手,在我移动布局中的按钮后,我的应用程序开始发生一些奇怪的事情。因此,有一个按钮(开始),单击该按钮会启动另一个 Activity 。这是代码。

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button startbutton = (Button) findViewById(R.id.button1);

startbutton.setOnClickListener(new View.OnClickListener() { //STARTBUTTON
@Override
public void onClick(View arg0) { //LO QUE HACE EL START BUTTON
Intent intent = new Intent(MainActivity.this, JugarActivity.class);
startActivity(intent);
}
});
}
}

我的问题是,在测试我的应用程序时,当我单击开始按钮时没有任何反应,但是当我单击布局中的另一个按钮(button4)时,它确实会将我发送到第二个 Activity 。这是我的 Activity_main.xml,以备需要时使用。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:minWidth="200dp"
android:text="@string/Start"
android:textSize="30sp" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="200dp"
android:minHeight="50dp"
android:minWidth="200dp"
android:text="@string/Jugarporcontinente"
android:textSize="30sp" />

<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="@string/Aprende"
android:textSize="30sp" />

<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="@string/Puntajes"
android:textSize="30sp" />

<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="@string/Opciones"
android:textSize="30sp" />

</LinearLayout>

我不明白当单击一个甚至没有代码的按钮时,如何从另一个按钮启动代码。欢迎任何帮助。谢谢。

最佳答案

根据问题评论,发生的情况如下:

编译资源时,资源 XML 文件被编译为二进制 XML 格式,并在 R.java 中生成资源标识符。生成的标识符指向资源二进制文件中的特定项目。标识符只是序列中的整数,其值按照它们在资源 XML 文件中声明的顺序给出。

大多数时候这都可以正常工作。当您修改源 XML 文件时,所做的更改将编译到二进制资源和 R 类中。

但是,有时构建依赖系统无法正常工作。例如,R 中的标识符已更新,但并非所有二进制资源都反射(reflect)更改后的标识符。当您尝试在代码中通过 id 引用元素时,它实际上返回二进制资源中使用该 id 指定的其他一些元素。

在这种情况下,你很“幸运” - 层次结构中存在该 id 的 View (没有返回 null,也没有 NullPointerException),而且它恰好是一个 Button(没有ClassCastException)。

清理项目会删除已编译的资源二进制文件和生成的 R.java,因此重新编译时,它们会再次同步。

关于java - Button2 正在做 Button1 应该做的事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20693397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com