0

I'm doing a switch case into onOptionsItemSelected, and i want to assign a title value for this item, but it don't works.

The problem apears in the case R.id.numTaula, when I use menu.Android says:

Qualifier must be an expresion.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title

    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    // Handle action bar actions click
    switch (item.getItemId()) {
        case R.id.action_settings:
            return true;

        case R.id.carrito:
            carrito c = new carrito();
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.frame_container, c);
            ft.commit();
            return true;

        case R.id.alerta:
            AlertDialog.Builder cambrer = new AlertDialog.Builder(this);

            cambrer.setTitle("Demanar cambrer");
            cambrer.setMessage("Està seguir que desitja l'atenció del cambrer?");


            cambrer.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    alertaCambrer = true;
                }
            });

            cambrer.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    alertaCambrer = false;
                }
            });

            cambrer.show();
            break;

        case R.id.numTaula:

            AlertDialog.Builder AlertTaula = new AlertDialog.Builder(this);

            AlertTaula.setTitle("Numero de taula");
            AlertTaula.setMessage("Introdueix el numero de taula");

            // Set an EditText view to get user input
            final EditText input = new EditText(this);
            input.setInputType(InputType.TYPE_CLASS_NUMBER);
            input.setRawInputType(Configuration.KEYBOARD_12KEY);
            AlertTaula.setView(input);

            AlertTaula.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    numTaula = input.getText().toString();
                    menu.findItem(R.id.numTaula).setTitle(numTaula);

                }

            });


            AlertTaula.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.
                }
            });


            AlertTaula.show();
            break;
        default:
            return super.onOptionsItemSelected(item);
    }return super.onOptionsItemSelected(item);
}
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195

2 Answers2

0

In the line:

numTaula = input.getText().toString();

Where exatcly you define what is numTaule? It is String?

yshahak
  • 4,996
  • 1
  • 31
  • 37
  • Yes it's an String, this is not the fault, because if i do a toast with this value, it works perfectly, but when try to use menu. it dont works. –  Mar 12 '15 at 23:00
0

That's the problem probably: https://stackoverflow.com/a/9093594/535762

Do you have a library project? If so, don't use a switch for resource ids, they aren't constants anymore.

EDIT:

Ok, I misread your problem, so it's because of some type problem.

But first try it like this:

EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setRawInputType(Configuration.KEYBOARD_12KEY);
AlertTaula.setView(input);

AlertTaula.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        public EditText input;

        // init method to set init values
        public DialogInterface.OnClickListener init(EditText input) {
            this.input = input;
            return this;
        }

        public void onClick(DialogInterface dialog, int whichButton) {
             menu.findItem(R.id.numTaula).setTitle(input.getText().toString());
        }

}.init(input));

You don't need final stuff for your interface implementations.

Then what exactly is numTaula? It seems to be a number? Is the type really a String? Post the code.

Community
  • 1
  • 1
einschnaehkeee
  • 1,858
  • 2
  • 17
  • 20
  • see i'm using switch into onOptionsItemSelected, and the other elements work correctly. The problem appears when i try to change the title value of this item. –  Mar 12 '15 at 23:05
  • see my edit, perhaps it will help you to narrow down the cause of the error. – einschnaehkeee Mar 12 '15 at 23:33
  • I have the same problem in this part of the code menu.findItem(R.id.numTaula).setTitle(input.getText().toString()); menu appears in red and android studio says "Qualifier must be an expresion" –  Mar 13 '15 at 00:36