todaroa todaroa - 2 months ago 25
Android Question

OnClickListener in Android Studio

I'm attempting to develop and app using the new Android Studio, but I keep receiving major errors on my OnClickListeners. Mainly it is telling me that it cannot resolve symbol "setOnClickListener" and it also cannot resolve "View v"

package com.sigmachi.derbydays;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
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 button= (Button) findViewById(R.id.standingsButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,StandingsActivity.class));
}
});


That is the code in the class

Information:Compilation completed with 11 errors and 0 warnings in 4 sec
Information:11 errors
Information:0 warnings
/Users/angelo/AndroidStudioProjects/SigmaChiDerbyDaysProject/SigmaChiDerbyDays/src/main/java/com/sigmachi/derbydays/MainActivity.java
Error:Error:line (28)Gradle: <identifier> expected
Error:Error:line (28)Gradle: illegal start of type
Error:Error:line (28)Gradle: ')' expected
Error:Error:line (28)Gradle: ';' expected
Error:Error:line (28)Gradle: invalid method declaration; return type required
Error:Error:line (30)Gradle: illegal start of type
Error:Error:line (30)Gradle: ';' expected
Error:Error:line (30)Gradle: ')' expected
Error:Error:line (30)Gradle: not a statement
Error:Error:line (30)Gradle: ';' expected
Error:Error:line (33)Gradle: illegal start of type


Those are the errors I am receiving which makes absolutely no sense. Line 28 starts at when I do
button.setOnClickListener


EDIT: Now I receive a force close when I press the button

This is the class it should open, a bare class with the only change being the layout to open

package com.sigmachi.derbydays;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class StandingsActivity extends Activity {

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


@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;
}

}

Answer
Button button= (Button) findViewById(R.id.standingsButton);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        startActivity(new Intent(MainActivity.this,StandingsActivity.class));
    }
});

This code is not in any method. If you want to use it, it must be within a method like OnCreate()

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

Button button= (Button) findViewById(R.id.standingsButton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,StandingsActivity.class));
            }
        });
        }

(sorry for the indentation)

Comments