Domanda

I am trying to program a sort of 'menu' in android with 3 buttons, and OnClickListeners recording input from each. However, I am getting some strange syntax errors.

Here is my MainActivity.java:

package com.example.galaxydefense;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

Button buttonplay=(Button)findViewById(R.id.buttonplay);
Button buttonhelp=(Button)findViewById(R.id.buttonhelp);
Button buttoncredits=(Button)findViewById(R.id.buttoncredits);
buttonplay.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Thread play=new Thread() {
            @Override
            public void run() {
                try {
                    Intent play=new Intent("android.intent.action.PLAY");
                    startActivity(play);
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
                finally {
                    finish();
                }
            }
        };
    }
});
buttonhelp.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Thread help=new Thread() {
            @Override
            public void run() {
                try {
                    Intent help=new Intent("android.intent.action.HELP");
                    startActivity(help);
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
                finally {
                    finish();
                }
            }
        };
    }
}
);
buttoncredits.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Thread credits=new Thread() {
            @Override
            public void run() {
                try {
                    Intent credits=new Intent("android.intent.action.CREDITS");
                    startActivity(credits);
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
                finally {
                    finish();
                }
            }
        };
    }
}
);
}

I am receiving the following errors:

line 24 - Syntax error on token "}", delete this token

line 94 - Syntax error, insert "}" to complete ClassBody

I am not sure if this error actually is a syntax error or just another error incorrectly diagnosed by the compiler.

È stato utile?

Soluzione

You can not write listener out of function. Replace your code with following code :

package com.example.galaxydefense;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button buttonplay=(Button)findViewById(R.id.buttonplay);
    Button buttonhelp=(Button)findViewById(R.id.buttonhelp);
    Button buttoncredits=(Button)findViewById(R.id.buttoncredits);
    buttonplay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Thread play=new Thread() {
                @Override
                public void run() {
                    try {
                        Intent play=new Intent("android.intent.action.PLAY");
                        startActivity(play);
                    }
                    catch(Exception e) {
                        e.printStackTrace();
                    }
                    finally {
                        finish();
                    }
                }
            };
        }
    });
    buttonhelp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Thread help=new Thread() {
                @Override
                public void run() {
                    try {
                        Intent help=new Intent("android.intent.action.HELP");
                        startActivity(help);
                    }
                    catch(Exception e) {
                        e.printStackTrace();
                    }
                    finally {
                        finish();
                    }
                }
            };
        }
    }
    );
    buttoncredits.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Thread credits=new Thread() {
                @Override
                public void run() {
                    try {
                        Intent credits=new Intent("android.intent.action.CREDITS");
                        startActivity(credits);
                    }
                    catch(Exception e) {
                        e.printStackTrace();
                    }
                    finally {
                        finish();
                    }
                }
            };
        }
    }
    );
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

Altri suggerimenti

You cannot put code directly inside the class as your doing. Use instead a constructor or an initializer block.

What your doing is define 3 fields in MainActivity : (buttonPlay, buttonHelp, buttonCredits) : This is ok. But then you're not allowed to write code directly as you do.

You can circumvent this behavior by wrapping your code after your fields inside an initializer block like this :

{ /* This is an initializer block ... */
buttonplay.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Thread play=new Thread() {
            @Override
            public void run() {
                try {
                    Intent play=new Intent("android.intent.action.PLAY");
                    startActivity(play);
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
                finally {
                    finish();
                }
            }
        };
    }
});
buttonhelp.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Thread help=new Thread() {
            @Override
            public void run() {
                try {
                    Intent help=new Intent("android.intent.action.HELP");
                    startActivity(help);
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
                finally {
                    finish();
                }
            }
        };
    }
}
);
buttoncredits.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Thread credits=new Thread() {
            @Override
            public void run() {
                try {
                    Intent credits=new Intent("android.intent.action.CREDITS");
                    startActivity(credits);
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
                finally {
                    finish();
                }
            }
        };
    }
}
);
/* Initializer block finishes here */ }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top