launch activity from ImageButton

1

I have the following code:

    public void BotonAjustes()
{
    View.OnClickListener listSet = new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_LONG).show();
            Intent intent= new Intent(MainActivity.this, Ajustes.class);
            startActivity(intent);
        }
    };
    boton= (ImageButton) findViewById(R.id.imageButton7);
    boton.setOnClickListener(listSet);
}

I know that the listener itself is fine, because when it only had the Toast it worked, but not now. What can I do to make it work? It does not give any compilation error.

EDIT

Part of the logcat:

07-20 18:21:27.279 1534-1544/? E/RecoverySystemService: Timed out connecting to uncrypt socket
07-20 18:21:27.279 1534-1544/? E/RecoverySystemService: Failed to connect to uncrypt socket
07-20 18:21:27.280 2231-3373/? E/SystemUpdateService: Failed to call RecoverySystem.cancelScheduledUpdate
                                                      java.lang.reflect.InvocationTargetException
                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                          at xae.c(:com.google.android.gms:134)
                                                          at afuw.d(:com.google.android.gms:195)
                                                          at afuw.p(:com.google.android.gms:2178)
                                                          at afuw.a(:com.google.android.gms:448)
                                                          at afuw.doInBackground(:com.google.android.gms:50475)
                                                          at android.os.AsyncTask$2.call(AsyncTask.java:304)
                                                          at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                          at java.lang.Thread.run(Thread.java:761)
                                                       Caused by: java.io.IOException: cancel scheduled update failed
                                                          at android.os.RecoverySystem.cancelScheduledUpdate(RecoverySystem.java:555)
                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                          at xae.c(:com.google.android.gms:134) 
                                                          at afuw.d(:com.google.android.gms:195) 
                                                          at afuw.p(:com.google.android.gms:2178) 
                                                          at afuw.a(:com.google.android.gms:448) 
                                                          at afuw.doInBackground(:com.google.android.gms:50475) 
                                                          at android.os.AsyncTask$2.call(AsyncTask.java:304) 
                                                          at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                                                          at java.lang.Thread.run(Thread.java:761) 
07-20 18:21:27.280 2231-3373/? E/SystemUpdateTask: exception trying to cancel scheduled update
                                                   java.io.IOException: Failed to invoke RecoverySystem.cancelScheduledUpdate
                                                       at xae.c(:com.google.android.gms:140)
                                                       at afuw.d(:com.google.android.gms:195)
                                                       at afuw.p(:com.google.android.gms:2178)
                                                       at afuw.a(:com.google.android.gms:448)
                                                       at afuw.doInBackground(:com.google.android.gms:50475)
                                                       at android.os.AsyncTask$2.call(AsyncTask.java:304)
                                                       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                       at java.lang.Thread.run(Thread.java:761)

Settings code:

public class Ajustes extends Activity 
{
    protected void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.set);
    }
}

Settings layout code (settings):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_green_dark"
        android:minHeight="?attr/actionBarSize"
        android:visibility="visible"
        >

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/toolbar"
            android:layout_gravity="center"
            android:textColor="@android:color/background_dark"
            android:textSize="18sp"
            android:textStyle="bold"
            />
    </android.support.v7.widget.Toolbar>

    <ImageButton
        android:id="@+id/back"
        android:layout_width="61dp"
        android:layout_height="56dp"
        app:srcCompat="?attr/actionModeCloseDrawable"
        tools:backgroundTint="@android:color/holo_green_dark"
        tools:ignore="ContentDescription" />

    <LinearLayout
        android:layout_width="360dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="1">

        <TextView
            android:id="@+id/Configuración"
            android:layout_width="384dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.48"
            android:ems="10"
            android:inputType="textPersonName"
            android:textAlignment="center"
            android:textColor="@android:color/background_dark"
            android:textSize="18sp"
            android:textStyle="bold"
            tools:ignore="TextViewEdits" />

        <RelativeLayout
            android:id="@+id/AutoLayout"
            android:layout_width="wrap_content"
            android:layout_height="77dp">

            <TextView
                android:id="@+id/auto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="24dp"
                android:layout_marginStart="24dp"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="@string/titulo1"
                android:textColor="@android:color/background_dark"
                android:textSize="18sp"
                android:textStyle="bold"
                tools:ignore="TextViewEdits" />

            <TextView
                android:id="@+id/textView9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/autoarranuqe"
                android:layout_toLeftOf="@+id/interruptor"
                android:layout_toStartOf="@+id/interruptor"
                android:text="@string/text1"
                android:textAlignment="center" />

            <Switch
                android:id="@+id/int"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:checked="false"
                android:textCursorDrawable="@android:color/holo_red_dark"
                tools:checked="false" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/WIFILayout"
            android:layout_width="match_parent"
            android:layout_height="77dp">

            <TextView
                android:id="@+id/editText16"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="22dp"
                android:layout_marginStart="22dp"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="@string/t2"
                android:textColor="@android:color/background_dark"
                android:textSize="18sp"
                android:textStyle="bold"
                tools:ignore="TextViewEdits" />

            <TextView
                android:id="@+id/Wifi"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentEnd="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/editText16"
                android:text="TextView"
                android:textAlignment="center"
                tools:text="@string/texto2"
                tools:ignore="HardcodedText" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/PlayerIDLayout"
            android:layout_width="match_parent"
            android:layout_height="103dp"
            android:layout_weight="0.13">

            <EditText
                android:id="@+id/playerID"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/gr"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:text="@string/texto3"
                android:textColor="?android:attr/textColorTertiary"
                android:textStyle="italic"
                android:inputType="" />

            <TextView
                android:id="@+id/editText17"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="27dp"
                android:layout_marginStart="27dp"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="@string/t3"
                android:textColor="@android:color/background_dark"
                android:textSize="18sp"
                android:textStyle="bold"
                tools:ignore="TextViewEdits" />

            <Button
                android:id="@+id/gr"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/editText17"
                android:background="@android:color/holo_green_dark"
                android:text="@string/bot1"
                android:textColor="@android:color/background_light" />

            <Button
                android:id="@+id/br"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/gr"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignStart="@+id/gr"
                android:layout_below="@+id/gr"
                android:text="@string/bot2" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/ILayout"
            android:layout_width="match_parent"
            android:layout_height="130dp"
            android:layout_weight="0.57">

            <RadioGroup
                android:id="@+id/radio"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:weightSum="1"
                tools:ignore="UselessParent">

                <TextView
                    android:id="@+id/textView14"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/t0"
                    android:textAlignment="center"
                    android:textColor="@android:color/background_dark"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <RadioButton
                    android:id="@+id/Fra "
                    android:layout_width="wrap_content"
                    android:layout_height="54dp"
                    android:checked="false"
                    tools:text="@string/tex" />

                <RadioButton
                    android:id="@+id/En"
                    android:layout_width="wrap_content"
                    android:layout_height="44dp"
                    android:checked="true"
                    tools:text="@string/texto5"
                    android:layout_weight="0.17" />

                <RadioButton
                    android:id="@+id/Es"
                    android:layout_width="wrap_content"
                    android:layout_height="48dp"
                    android:layout_weight="0.41"
                    android:checked="true"
                    tools:text="@string/text4" />
            </RadioGroup>
        </RelativeLayout>
    </LinearLayout>
</FrameLayout>
    
asked by pepito 20.07.2017 в 17:15
source

2 answers

0

Try this way:

         ImageButton imageButton;
         imageButton = (ImageButton) findViewById(R.id.imageButton);
         imageButton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
            Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_LONG).show();
            Intent intent= new Intent(MainActivity.this, Ajustes.class);
            startActivity(intent);
         }
    });
    
answered by 20.07.2017 в 17:31
0

If you define a listener in this way:

   View.OnClickListener listSet = new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_LONG).show();
                Intent intent= new Intent(MainActivity.this, Ajustes.class);
                startActivity(intent);
            }
        };

set that listener to your button, for example:

boton.setOnClickListener(listSet);

in this way it would be called the listener when the button is pressed.

To launch the Activity , your code is correct:

  Intent intent= new Intent(MainActivity.this, Ajustes.class);
  startActivity(intent);

but remember that the Adjustments class should extend from Activity , FragmentActivity or AppCompatActivity and you also have to register within your file AndroidManifest.xml

example:

<?xml version="1.0" encoding="UTF-8"?>     
<manifest>

    <application
        ...
        ...

        <!-- Declaracion de Activity-->
        <activity
            android:name=".Ajustes">


    </application>
</manifest>
    
answered by 20.07.2017 в 17:50