开发者

Illegal start of expression?

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 an开发者_JAVA百科droid.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


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.


One thing I noticed, you don't need:

clickerNumber = clickerNumber++;

because the ++ operator handles assignment. Try simply:

clickerNumber++;

Though I should mention this isn't the cause of your problem, just a tip I guess.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜