Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 849659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:08:21+00:00 2026-05-15T07:08:21+00:00

I’m trying to build a simple Android app that increments a number displayed every

  • 0

I’m trying to build a simple Android app that increments a number displayed every time a button is pressed, but I can’t work out how to fix the “illegal start of expression” error I keep getting.

My code:

package com.clicker;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class Clicker extends Activity
{
    private int clickerNumber = 0;
    private TextView clickerText;
    private Button clickerButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        clickerText = (TextView)findViewById(R.id.clickerText);
        final Button clickerButton = (Button)findViewById(R.id.clickerButton);
             clickerButton.setOnClickListener(new View.OnClickListener());
                {
                 public void onClick();
                    {
                    clickerNumber = clickerNumber++;
                    clickerText.setText(Integer.toString(clickerNumber));
                    }
                }
    }
}

And compiler output:

compile:
[javac] Compiling 2 source files to /home/fraser/Applications/Android/Code/Clicker/bin/classes
[javac] /home/fraser/Applications/Android/Code/Clicker/src/com/clicker/Clicker.java:24: ')' expected
[javac]              clickerButton.setOnClickListener(new View.OnClickListener();
[javac]                                                                         ^
[javac] /home/fraser/Applications/Android/Code/Clicker/src/com/clicker/Clicker.java:26: illegal start of expression
[javac]                  public void onClick();
[javac]                  ^
[javac] /home/fraser/Applications/Android/Code/Clicker/src/com/clicker/Clicker.java:26: illegal start of expression
[javac]                  public void onClick();
[javac]                         ^
[javac] /home/fraser/Applications/Android/Code/Clicker/src/com/clicker/Clicker.java:26: ';' expected
[javac]                  public void onClick();
[javac]                                     ^
[javac] /home/fraser/Applications/Android/Code/Clicker/src/com/clicker/Clicker.java:29: ';' expected
[javac]                     clickerText.setText(Integer.toString(clickerNumber)));
[javac]                                                                         ^
[javac] 5 errors
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:08:22+00:00Added an answer on May 15, 2026 at 7:08 am

    You have some unwanted semicolons:

    clickerButton.setOnClickListener(new View.OnClickListener());
    
    public void onClick();
    

    To fix the syntax errors you want something like this:

    clickerButton.setOnClickListener(new View.OnClickListener() {
        public void onClick() {
            clickerNumber = clickerNumber + 1;
            clickerText.setText(Integer.toString(clickerNumber));
        }
    });
    

    Your indentation seems to be wrong which might be partly what caused the confusion. Use the automatic indentation of your editor to catch a lot of these types of errors.

    But please note that the above code still will not work because inside an anonymous class you cannot access to local variables that are not final. So you cannot access clickerNumber. If you make it final that doesn’t help you much either because then you can’t change the value of it. You could promote the counter to a member variable.

    I’d also recommend that before attempting to write an Android application you first follow a standard Java tutorial and make sure you understand the basic syntax of Java. You will find that most Android tutorials will assume that you already have a good understanding of writing standard Java applications.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.