简体   繁体   中英

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.

For the Java page it is saying: "Could not find method buttonAbout1(View) in a parent or ancestor Context for android:onClick attribute"

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"

I have my code shown below.

Thank you in advance!

Main 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"/>

My Java page for the main activity (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. Try this line instead:

Update: Oh I just see, you connect your button twice to the onclick. Either you do it in code or you to it in xml. Here is solution for xml. Change the buttonAbout1 method

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

1. First you need to make buttonAbout1 method public.

2.And then you need to pass View as a parameters.Like this

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.

Hope this help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM