onClick in the menu [closed]

0

Good afternoon, in my application I have a menu with two items and they are ifRoom, when I put my save function to start it shows an error in LogCat but I do not know how to solve it ... could anyone help me please. p>

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

But I do not 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) 
    
asked by anonymous 16.10.2016 / 22:12

1 answer

2
The java.lang.NoSuchMethodException: 'myMethod ()' error occurs when the meuMetodo() method was not declared within its class / interface.

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

meuMetodo(View v){

 //Código do seu método aqui

}

So, as stated in the comments, you need to have a method SalvarPartida(View v) and Results(View v) in your Activity SimpleHome

    
17.10.2016 / 00:56