onClick on the menu

Asked

Viewed 84 times

0

Good Afternoon, in my application I have a menu with two items and are ifRoom, when I put my function to save the match it shows an error in Logcat but I do not know how to solve... someone could help me please.

NOTE: I know the error is at this point 'Caused by: java.lang.NoSuchMethodException: SalvarPartida [interface android.view.MenuItem'

but I don’t know what you mean...

code:

package com.allsport.miyonic.allsport;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import Base.*;

public class SimplesHome extends AppCompatActivity {

    private ImageButton imgButton_play, imgButton_pause, imgButton_1, imgButton_2;
    private Button saver, results;
    public TextView valorOne;
    public TextView valorDouble;
    public int contador = 0;
    public int contador1 = 0;
    private Chronometer reloginho;
    private EditText casa, fora;
    long tempoPausado = 0;

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



        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        imgButton_1 = (ImageButton) findViewById(R.id.imgButton_1);
        imgButton_2 = (ImageButton) findViewById(R.id.imgButton_2);
        imgButton_play = (ImageButton) findViewById(R.id.imgButton_play);
        imgButton_pause = (ImageButton) findViewById(R.id.imgButton_pause);
        reloginho = (Chronometer) findViewById(R.id.chronometer);
        valorOne = (TextView) findViewById(R.id.txt_valor1);
        valorDouble = (TextView) findViewById(R.id.txt_valor2);
        casa = (EditText) findViewById(R.id.lbl_time1);
        fora = (EditText) findViewById(R.id.lbl_time2);
        saver = (Button) findViewById(R.id.save);
        results = (Button) findViewById(R.id.btnr);


        imgButton_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contador++;
                valorOne.setText("" + contador);
            }
        });

        imgButton_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contador1++;
                valorDouble.setText("" + contador1);
            }
        });

        imgButton_play.setEnabled(true);
        imgButton_pause.setEnabled(false);
        imgButton_1.setEnabled(false);
        imgButton_2.setEnabled(false);


        imgButton_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgButton_play.setEnabled(false);
                imgButton_pause.setEnabled(true);
                imgButton_1.setEnabled(true);
                imgButton_2.setEnabled(true);

                reloginho.setBase(SystemClock.elapsedRealtime());
                reloginho.start();
                reloginho.setBase(SystemClock.elapsedRealtime() + tempoPausado);
            }
        });

        imgButton_pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgButton_play.setEnabled(true);
                imgButton_pause.setEnabled(false);
                imgButton_1.setEnabled(false);
                imgButton_2.setEnabled(false);

                tempoPausado = reloginho.getBase();
                SystemClock.elapsedRealtime();
                reloginho.stop();
            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.salvarpartida) {

                Esporte inserir = new Esporte();

                inserir.setNomeTimeUm(casa.getText().toString());
                inserir.setNomeTimeDois(fora.getText().toString());
                inserir.setValorUm(Integer.parseInt(valorOne.getText().toString()));
                inserir.setValorDois(Integer.parseInt(valorDouble.getText().toString()));

                DbHelper dbhelp = new DbHelper(this);
                dbhelp.insertResultado(inserir);

                finish();
        }

        if (id == R.id.results){
            Intent resul = new Intent(this, ResultSimples.class);
            startActivity(resul);

            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/salvarpartida"
    android:title="@string/salvar_partida"
    android:orderInCategory="100"
    android:onClick="SalvarPartida"
    app:showAsAction="ifRoom" />

    <item
        android:id="@+id/results"
        android:title="Resultados"
        android:orderInCategory="100"
        android:onClick="Results"
        app:showAsAction="ifRoom"  />
</menu>

Logcat:

*E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.allsport.miyonic.allsport, PID: 4433
                  android.view.InflateException: Couldn't resolve menu item onClick handler SalvarPartida in class com.allsport.miyonic.allsport.SimplesHome
                      at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:242)
                      at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:443)
                      at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:479)
                      at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:196)
                      at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:118)
                      at com.allsport.miyonic.allsport.SimplesHome.onCreateOptionsMenu(SimplesHome.java:107)
                      at android.app.Activity.onCreatePanelMenu(Activity.java:2823)
                      at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:340)
                      at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
                      at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258)
                      at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1320)
                      at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1600)
                      at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:92)
                      at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:131)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                   Caused by: java.lang.NoSuchMethodException: SalvarPartida [interface android.view.MenuItem]
                      at java.lang.Class.getMethod(Class.java:664)
                      at java.lang.Class.getMethod(Class.java:643)
                      at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:240)
                      at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:443) 
                      at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:479) 
                      at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:196) 
                      at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:118) 
                      at com.allsport.miyonic.allsport.SimplesHome.onCreateOptionsMenu(SimplesHome.java:107) 
                      at android.app.Activity.onCreatePanelMenu(Activity.java:2823) 
                      at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:340) 
                      at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85) 
                      at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258) 
                      at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1320) 
                      at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1600) 
                      at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:92) 
                      at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:131) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
  • In which version of Android this error happened?

  • @Nathan Where is the method Salvarpartida() ?

  • And where are defined these functions "Salvarpartida" and "Results"?

  • @Pabloalmeida It took place in the 6.0 version and I did not get to do the functions I was seeing on the internet save action in the menu was only put what I wanted inside the if as follows to open another Activity Resultsimple

  • @Gamen did not get to do... as well as the method?

  • @Nathan Remove these Onclick attributes from your XML and see if the error remains.

  • worked thanks....

  • @Nathan in your xml you call a nonexistent method called SalvarPartida(), therefore the mistake.

Show 3 more comments

1 answer

2


The mistake java.lang.Nosuchmethodexception: 'meuMetodo()' occurs when the method meuMetodo() not declared inside its class / interface.

In your case, every time you put the line android:onClick="meuMetodo" in an XML layout file, you need to create a method like the one below in Activity / Fragment that uses this layout

meuMetodo(View v){

 //Código do seu método aqui

}

Therefore, as stated in the comments, you need to have a method SalvarPartida(View v) and Results(View v) in its Activity Simpleshome

Browser other questions tagged

You are not signed in. Login or sign up in order to post.