OnClickListener in Android Studio

2020-02-04 11:06发布

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

}

标签: java android
5条回答
ら.Afraid
2楼-- · 2020-02-04 11:41

This worked for me:

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

    btnSave = (Button)findViewById(R.id.btnSave);

    OnClickListener btnListener = new OnClickListener() {
        @Override
        public void onClick(android.view.View view) {
            finish();
        }
    };
    btnSave.setOnClickListener(btnListener);

}
查看更多
家丑人穷心不美
3楼-- · 2020-02-04 11:45
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));
        }
    });
}
查看更多
干净又极端
4楼-- · 2020-02-04 11:48
@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;
    }

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
         int id = item.getItemId();
         if (id == R.id.standingsButton) {
            startActivity(new Intent(MainActivity.this,StandingsActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
查看更多
\"骚年 ilove
5楼-- · 2020-02-04 11:51

you will need to button initilzation inside method instead of trying to initlzing View's at class level do it as:

 Button button;  //<< declare here..

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button= (Button) findViewById(R.id.standingsButton); //<< initialize here
         // set OnClickListener for Button here
        button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this,StandingsActivity.class));
        }
      });
    }
查看更多
成全新的幸福
6楼-- · 2020-02-04 11:54
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_my);
    titolorecuperato = (TextView) findViewById(R.id.textView);
    String stitolo = titolorecuperato.getText().toString();

    Button btnHome = (Button) findViewById(R.id.button);

    btnHome.setOnClickListener(new View.OnClickListener() {

       @Override
        public void onClick(View view) {

       }
});

same thing as Nic007 said before.

You do need to write code inside "onCreate" method. Sorry me too for the indent... (first comment here)

查看更多
登录 后发表回答