the same code gives me an error

1

I'm trying to follow the steps of an example to create a horizontal menu, and the same code to me shows me an error.

This is the MainActivity code

package com.ejemplo.socu.menuhorizontal;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.HorizontalScrollView;
import android.widget.TextView;
import android.widget.Toast;
import com.darwindeveloper.horizontalscrollmenulibrary.custom_views.HorizontalScrollMenuView;

public class MainActivity extends AppCompatActivity {

    HorizontalScrollMenuView menu;
    TextView textView;

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

        menu = (HorizontalScrollMenuView) findViewById(R.id.menu);
        textView = (TextView)findViewById(R.id.txtText);

        //Create menu
        initMenu();
    }

    private void initMenu() {
        menu.addItem("Transaction",R.drawable.ic_money);
        menu.addItem("Payment",R.drawable.ic_payment);
        menu.addItem("Account",R.drawable.ic_acc);
        menu.addItem("Support",R.drawable.ic_done);

        menu.setOnHSMenuClickListener(new HorizontalScrollMenuView.OnHSMenuClickListener(){

            @Override
            public void onHSMClick(MenuItem menuItem, int position) {
                Toast.makeText(MainActivity.this, ""+menuItem.getText(), Toast.LENGTH_SHORT).show();
                textView.setText(menuItem.getText());
            }
        });
    }
}

And I am checking the following text with an error, and it is the same as in the example to create the menu: menu.setOnHSMenuClickListener (new HorizontalScrollMenuView.OnHSMenuClickListener () {

Greetings.

    
asked by SoCu 10.12.2017 в 11:13
source

2 answers

0

The error indicates that you are not implementing the OnHSMClick method because you are using the onHSMClick method. Replace the first one with an O mayuzcula:

  menu.setOnHSMenuClickListener(new HorizontalScrollMenuView.OnHSMenuClickListener(){


            @Override
            // la O debe de ser mayuscula
            public void OnHSMClick(MenuItem menuItem, int position) {
                Toast.makeText(MainActivity.this, ""+menuItem.getText(), Toast.LENGTH_SHORT).show();
                textView.setText(menuItem.getText());
            }
        });
    
answered by 11.12.2017 в 15:05
-1

In this case I think you're using a library: HorizontalScrollMenu ; the error refers to the lack of implementation of the onHSMClick() method, but in fact you are doing it correctly:

    menu.setOnHSMenuClickListener(new HorizontalScrollMenuView.OnHSMenuClickListener() {
        @Override
        public void onHSMClick(MenuItem menuItem, int position) {

                ....
                ....
        }
    });

If you are actually performing the implementation correctly, what would be the problem? well then the problem is that the class menuItem does not have the method getText() :

menuItem.getText()

But if we take a look at the library, this contains a public method to obtain the text ,

public String getText() {
    return text;
}

Therefore the problem is that you are not using the correct class MenuItem , the class should be the one of the library, do not use the one of the sdk that is:

import android.view.MenuItem;
    
answered by 11.12.2017 в 18:27