Button to close Activity in Android studio

0

I have a button on my second activity (pre-messages)

<Button
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:id="@+id/Button40"
    android:src="@drawable/back"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:focusable="true"
    android:onClick=""
    android:clickable="true" />

That I must close my activity to return to the primary activity, I am dealing with the code:

Button cerrar= (Button) findViewById(R.id.Button40); 
cerrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    finish(); 
}
});

At the moment of inserting it into the java file of my second activity just below:

package com.globalstar.st300r;

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


public class MensajesPredet extends Activity  {


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

}

Button cerrar = (Button) findViewById(R.id.Button40);
cerrar.setOnClickListener(new View.OnClickListener() {
    @Override
            public void onClick(View v)
        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.menu_mensajes_predet, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

And this is the java of my first activity:

package com.globalstar.st300r;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends Activity {


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

/** Called when the user clicks the Send button */
public void predefinido(View view) {
    Intent intent = new Intent(this, MensajesPredet.class);
            startActivity(intent);

}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

but it marks me 4 errors:

1) when I put it on, it detects me the @override as a comment and it marks the legend "Annotations are not allowed here"

2) in the code close. SetOnClickListener check (Can not resolve symbol) and SetOnclickListener in red

3) (View v) mark error

4) Onclick is never used

I would appreciate your support !!

    
asked by Pablo Vargas Marin 18.08.2017 в 20:04
source

2 answers

3

This is your code for the second activity:

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

}

Button cerrar = (Button) findViewById(R.id.Button40);
cerrar.setOnClickListener(new View.OnClickListener() {
@Override
    public void onClick(View v) 
       finish();

}

should be like this

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

   Button cerrar = (Button) findViewById(R.id.Button40);
   cerrar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            finish();

    });
}

all events associated with android controls must be initialized in the onCreate () method of their respective Activity

    
answered by 18.08.2017 / 21:07
source
0

The 4 problems are related to that you are declaring the button out of onCreate() which is where you load in layout activity_main.xml that contains the button; you have to add in the obtaining of the reference of the button and its listener:

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

    /* Boton */
    Button cerrar = (Button) findViewById(R.id.Button40);
    cerrar.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v)
         finish();

      });
    /*****/


}

But there is another detail, remove the android:onClick property from your layout, since the button has a listener for this purpose.

<Button
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:id="@+id/Button40"
    android:src="@drawable/back"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:focusable="true"

    android:clickable="true" />

If you want to use the android:onClick property, do not define the button listener:

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

/* Boton */
/*Button cerrar = (Button) findViewById(R.id.Button40);
cerrar.setOnClickListener(new View.OnClickListener() {
    @Override
            public void onClick(View v)
        finish();

});*/

}

only defines the method using the android:onClick property, for example finalizar :

and declare the method in code:

public void finalizar(View v){
    finish();
}

Actually, as you can see, there are 2 different ways to activate the click of a button!

    
answered by 18.08.2017 в 21:44