问题描述:

I encountered an issue and couldn't resolve in Android Studio. The setOnClickListener remains red and doesn't work unless I get rid of my "loseStarter1" button name.

Note: Starter1 is a button, I'm trying to make it disappear when clicked by the user. My real code starts when I introduce the loseStarter1 button.

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

public class game1 extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_game1);

}

Button loseStarter1;

loseStarter1 = (Button) findViewById(R.id.Starter1);

loseStarter1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

loseStarter1.setVisibility(View.GONE);

}

})

}

Much appreciated.

网友答案:

You're missing a semicolon to end the new View.OnClickListener() { ... statement as well as that block not being inside of a method.

Not only move this code into the onCreate method, make sure you end it with a semicolon.

loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        loseStarter1.setVisibility(View.GONE);
        }
    }); // Add the semicolon here

It should look like this:

public class game1 extends AppCompatActivity {

    Button loseStarter1;

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

        loseStarter1 = (Button) findViewById(R.id.Starter1);
        loseStarter1.setOnClickListener(new View.OnClickListener() {
            @Override
             public void onClick(View v) {
                loseStarter1.setVisibility(View.GONE);
            }
        }); //added semicolon

    } // ends onCreate method
} // ends class
网友答案:

Your Button variable declaration and OnClickListener initialization is outside of the onCreate() method. Use the following code:

package com.cutting_edge_tech.mentalenhancementapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class game1 extends AppCompatActivity {

    private Button loseStarter1;

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

        loseStarter1 = (Button) findViewById(R.id.Starter1);
        loseStarter1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loseStarter1.setVisibility(View.GONE);
                }
        });
    }
}
网友答案:

Move below code inside onCreate()

loseStarter1 = (Button) findViewById(R.id.Starter1);
    loseStarter1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            loseStarter1.setVisibility(View.GONE);
            }
        });
网友答案:

This is how i do it.

1) first make the class implement the interface View.OnClickListener, this let you handle the button clic event:

public class game1 extends AppCompatActivity implements View.OnClickListener;

2) second create the interface method to handle event.

@Override
public void onClick(View view)
{
    if(view.getId()==R.id.Starter1)
    {
       view.setVisibility(View.GONE);
    }
}

3) OnCreate methods is the best way to find objects and set properties.

@Override
protected void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_game1);

 loseStarter1= (Button) findViewById(R.id.Starter1);

 if(loseStarter1!=null){
   loseStarter1.setOnClickListener(this);
 }

}

all code :

package com.cutting_edge_tech.mentalenhancementapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class game1 extends AppCompatActivity implements View.OnClickListener  {

private Button loseStarter1;

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

      loseStarter1 = (Button) findViewById(R.id.Starter1);
      if(loseStarter1!=null){
        loseStarter1.setOnClickListener(this);
      }
  }

  @Override
  public void onClick(View view)
  {
     if(view.getId()==R.id.Starter1)
     {
      view.setVisibility(View.GONE);
     }
  }
}

Advice: Rename class to Game1.

相关阅读:
Top