Search code examples
javaandroidxml

I'm having problems with the button error


package com.zairy.app;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity { 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    Button toggle = findViewById(R.id.toggle);
    toggle.setOnClickListener(new View.onClickListener) {
        @Override
        public void onClick(View v) {
            Toast toast = Toast.makeText(this, "Alert", Toast.LENGTH_LONG);
        }
    });
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button is currently enabled"
        android:id="@+id/text" />
    <Button
        android:id="@+id/toggle"
        android:onClick="onClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="enabled"
        android:layout_below="@+id/text"
        />
</RelativeLayout>

Above me is the code provided, I'm having some errors on line 20-26. Here are the errors ⬇

ERROR: <identifier> expected [MainActivity.java:20]
ERROR: illegal start of type [MainActivity.java:20]
ERROR: ';' expected [MainActivity.java:22]
ERROR: ';' expected [MainActivity.java:22]
ERROR: illegal start of type [MainActivity.java:26]
ERROR: package toggle does not exist [MainActivity.java:20]
ERROR: illegal start of type [MainActivity.java:22]
ERROR: modifier public not allowed here [MainActivity.java:22]

I hope someone can help me with this.

I keep changing the code, fiddle it around expecting it to work. The other button onClick I used was working well (which is public void <the variable for the button id> (View v) {}) but when I used this other method, it didn't work, instead it gives me errors.


Solution

  • The way you have written code is wrong, let me correct it:

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button toggle = findViewById(R.id.toggle);
    
            toggle.setOnClickListener(new View.onClickListener) {
    
                @Override
                public void onClick(View v) {
                    Toast.makeText(this, "Alert", Toast.LENGTH_LONG).show();
                }
            });
    
        }
    }
    

    View initialization need to be placed in oncreate() method but not outside and if you want to write it outside of oncreate() method , then make another method and place your code inside it and call that method in onCreate() like below example:

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            anotherMethod();
        }
        
        private void anotherMethod() {
            Button toggle = findViewById(R.id.toggle);
            toggle.setOnClickListener(new View.onClickListener) {
    
                @Override
                public void onClick(View v) {
                    Toast.makeText(this, "Alert", Toast.LENGTH_LONG).show();
                }
    
            });
        }
    }