简体   繁体   English

如何获得我的按钮以在Android Studio中打开第二个活动?

[英]How to get my button to open a second activity in Android Studio?

I have read many other forums from others' questions related to this issue and still cannot get my code to work. 我已经从其他人与该问题有关的问题中阅读了许多其他论坛,但仍然无法使我的代码正常工作。 I cannot find anything wrong with my code yet I am getting an error on the back end (Java page) and the layout page. 我的代码找不到任何错误,但后端(Java页面)和布局页面却出现错误。

For the Java page it is saying: "Could not find method buttonAbout1(View) in a parent or ancestor Context for android:onClick attribute" 对于Java页面,它说:“在android:onClick属性的父级或祖先上下文中找不到方法buttonAbout1(View)”

And for the layout page it is saying: "Method 'buttonAbout1' in 'GMOEd' has incorrect signature. Checks if the method specified in onClick XML attribute is declared in related activity" 对于布局页面,它说:“'GMOEd'中的方法'buttonAbout1'具有不正确的签名。检查在相关活动中是否声明了onClick XML属性中指定的方法”

I have my code shown below. 我的代码如下所示。

Thank you in advance! 先感谢您!

Main Activity (activity_gmoed) 主要活动(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"/>

My Java page for the main activity (GMOEd.Java) 我的主要活动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));
            }
        });
        {


        }

Manifest Page: 清单页:

<?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)

Your buttonAbout1 method needs the right signature, means the correct parameters. 您的buttonAbout1方法需要正确的签名,表示正确的参数。 Try this line instead: 尝试以下这一行:

Update: Oh I just see, you connect your button twice to the onclick. 更新:哦,我刚刚看到,您将按钮两次连接到了onclick。 Either you do it in code or you to it in xml. 您可以在代码中进行操作,也可以在xml中进行操作。 Here is solution for xml. 这是xml的解决方案。 Change the buttonAbout1 method 更改buttonAbout1方法

private void buttonAbout1(View v) {
    startActivity(new Intent(GMOEd.this,About2.class));
}

1. First you need to make buttonAbout1 method public. 1.首先,您需要公开buttonAbout1方法。

2.And then you need to pass View as a parameters.Like this 2然后需要传递View作为参数

public void buttonAbout1(View v) {
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));
        }
    });
}

Here is a quick tip Avoid making onClick method by yourself.Instead try using android studio's intention action(ALT+ENTER) to generate for you.When you add android:onClick="buttonAbout1" in the xml ,then press ALT+ENTER(make sure your cursor located on onClick) and then choose Create 'buttonAbout1(View)' in GMOEd and that will create the method in your activity. 快速提示:避免自己制作onClick方法。而是尝试使用android studio的意图动作(ALT + ENTER)为您生成。当您在xml中添加android:onClick =“ buttonAbout1”时,请按ALT + ENTER(make确保光标位于onClick上),然后在GMOEd中选择“ 创建'buttonAbout1(View)” ,这将在您的活动中创建方法。

Hope this help! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得在Android Studio中打开新活动的按钮? - How do i get a button to open a new activity in android studio? 在android studio中的第二个活动中打开第三个活动 - open third activity in second activity in android studio 第二个活动上的 Android Studio 我的按钮不起作用 - Android Studio My Button on the second Activity doesn't Work 如何在我在android studio中的第二个活动上显示单击的单选按钮值? - how to display the clicked radio button value on my second activity in android studio? 如何在android studio中使用弹出窗口按钮打开新活动? - How to open new activity using popup window button in android studio? Android Studio:使按钮打开下一个活动 - Android Studio: Make a Button open Next Activity 如何从 Android Studio 中的主活动以外的默认活动点击按钮打开链接? - How to Open a link on button click From a Default Activity other than Main Activity in Android Studio? 单击第二个活动的按钮会自动关闭应用程序 [Android Studio] - Clicking the button to second activity automatically closes app [Android Studio] 如何在 Android 工作室上使用 html 打开新活动 - How to open a new activity with html on Android studio Android Studio:如何在我的第二个活动中看到intent方法携带的信息? - Android Studio: how can I see the information carried through the intent method on my second activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM