gpt4 book ai didi

java - 如何让我的按钮在 Android Studio 中打开第二个 Activity?

转载 作者:行者123 更新时间:2023-12-02 03:41:04 25 4
gpt4 key购买 nike

我已经阅读了许多其他论坛中其他人与此问题相关的问题,但仍然无法让我的代码正常工作。我找不到我的代码有任何问题,但后端(Java 页面)和布局页面出现错误。

对于 Java 页面,它说:“在 android:onClick 属性的父级或祖先上下文中找不到方法 buttonAbout1(View)”

对于布局页面,它说:“‘GMOEd’中的方法‘buttonAbout1’签名不正确。检查 onClick XML 属性中指定的方法是否在相关 Activity 中声明”

我的代码如下所示。

提前谢谢您!

主要 Activity (activity_gmoed)

<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About"
android:id="@+id/buttonAbout1"
android:background="#ffffff"
android:foregroundTint="#ffffff"
android:layout_below="@+id/textView2"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:onClick="buttonAbout1"/>

我的主要 Activity Java 页面 (GMOEd.Java)

public class GMOEd extends AppCompatActivity {

/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gmoed);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

private void buttonAbout1() {
Button buttonAbout1 = (Button) findViewById(R.id.buttonAbout1);
assert buttonAbout1 != null;
buttonAbout1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(GMOEd.this,About2.class));
}
});
{


}

list 页面:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.gmoed">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".GMOEd">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".About2"></activity>
<!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>

</manifest>

最佳答案

尝试将 private void buttonAbout1() 的参数更改为 private void buttonAbout1(View v)

关于java - 如何让我的按钮在 Android Studio 中打开第二个 Activity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36829836/

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