简体   繁体   中英

onClick event don't work Android

I have an error, pretty common as I have seen several solutions, all the same on the internet. I have tried to do this on my android code but it doesn't work.

I have a button. This button, depending on what text is on a spinner, is supposed to make me go to a page or another.

This is the xml code of the button:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lancer"
    android:id="@+id/startButton"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:clickable="true"
    android:onClick="jumpTo"/>

and its java class:

public class Admin extends AppCompatActivity implements View.OnClickListener {

Spinner spinner_event;
Spinner spinner_activity;
Spinner spinner_wallpaper;
ArrayAdapter<CharSequence> adapter_wallpaper;
ArrayAdapter<CharSequence> adapter_event;
ArrayAdapter<CharSequence> adapter_activity;
Button startButton;
String textActivity;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.content_admin);


    spinner_event = (Spinner)findViewById(R.id.spinner_event);
    adapter_event = ArrayAdapter.createFromResource(this, R.array.events,android.R.layout.simple_spinner_dropdown_item);
    spinner_event.setAdapter(adapter_event);


    spinner_activity = (Spinner)findViewById(R.id.spinner_activity);
    adapter_activity = ArrayAdapter.createFromResource(this, R.array.activity,android.R.layout.simple_spinner_dropdown_item);
    spinner_activity.setAdapter(adapter_activity);


    spinner_wallpaper = (Spinner)findViewById(R.id.spinner_wallpaper);
    adapter_wallpaper = ArrayAdapter.createFromResource(this, R.array.wallpapers,android.R.layout.simple_spinner_dropdown_item);
    spinner_wallpaper.setAdapter(adapter_wallpaper);

    startButton = (Button)findViewById(R.id.startButton);
    startButton.setOnClickListener(this);
    }

@Override
public void onClick(View v) {
    textActivity = spinner_activity.getSelectedItem().toString();
    switch(v.getId()){

        case R.id.startButton:
        {
            if(textActivity == "Entree"){
                Intent i = new Intent(this, BadgeEntree.class);
                startActivity(i);
            }
            break;
        }
    }
}
}

I am supposed to go to my BadgeEntree. Both of them are defined on the AndroidManifest.xml, I am pretty lost everything seems is ok but it isn't, what am I doing wrong?

Thanks for the help

Remove

     startButton.setOnClickListener(this);
    }

@Override
public void onClick(View v) {
    textActivity = spinner_activity.getSelectedItem().toString();
    switch(v.getId()){

        case R.id.startButton:
        {
            if(textActivity == "Entree"){
                Intent i = new Intent(this, BadgeEntree.class);
                startActivity(i);
            }
            break;
        }
    }

Do

At first You should call if(textActivity.equals("Entree") instead of ==

FYI

Now, when you clicks the button, the Android system calls the activity's jumpTo(View) method. In order for this to work, the method must be public and accept a View as its only parameter.

public void jumpTo(View i)
{
textActivity = spinner_activity.getSelectedItem().toString();
if(textActivity.equals("Entree"){
                Intent i = new Intent(this, BadgeEntree.class);
                startActivity(i);
            }
            else
            {
            Log.d("Else","Wrong");
            }
}

Finally

public class Admin extends AppCompatActivity  {

Spinner spinner_event;
Spinner spinner_activity;
Spinner spinner_wallpaper;
ArrayAdapter<CharSequence> adapter_wallpaper;
ArrayAdapter<CharSequence> adapter_event;
ArrayAdapter<CharSequence> adapter_activity;
Button startButton;
String textActivity;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.content_admin);


    spinner_event = (Spinner)findViewById(R.id.spinner_event);
    adapter_event = ArrayAdapter.createFromResource(this, R.array.events,android.R.layout.simple_spinner_dropdown_item);
    spinner_event.setAdapter(adapter_event);


    spinner_activity = (Spinner)findViewById(R.id.spinner_activity);
    adapter_activity = ArrayAdapter.createFromResource(this, R.array.activity,android.R.layout.simple_spinner_dropdown_item);
    spinner_activity.setAdapter(adapter_activity);


    spinner_wallpaper = (Spinner)findViewById(R.id.spinner_wallpaper);
    adapter_wallpaper = ArrayAdapter.createFromResource(this, R.array.wallpapers,android.R.layout.simple_spinner_dropdown_item);
    spinner_wallpaper.setAdapter(adapter_wallpaper);

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

    }


public void jumpTo(View i)
{
textActivity = spinner_activity.getSelectedItem().toString();
if(textActivity.equals("Entree"){
                Intent i = new Intent(this, BadgeEntree.class);
                startActivity(i);
            }
            else
            {
            Log.d("Else","Wrong");
            }
}


}

从按钮xml代码中删除onClick标记

Remove android:onClick="jumpTo" in xml

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lancer"
android:id="@+id/startButton"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:clickable="true"
/>

You must delete this line

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lancer"
    android:id="@+id/startButton"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:clickable="true"
    android:onClick="jumpTo" <-----

/>

and move logic for jumpTo method in your class because now you have onCLick listener in you class and in your xml. Also change this line

if(textActivity == "Entree")  

with this..

if(textActivity.equals("Entree"))
if(textActivity == "Entree")  

change above code to

if(textActivity.equalsIgnoreCase("Entree"))

also remove below code from ur xml

android:onClick="jumpTo" 

NOTE: You can use "equalsIgnoreCase" if u want Case Insensitive comparison of strings, If You want Case Sensitive comparision of strings then you can use "equals".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM