1

The onCreateOptionsMenu override is giving me an error. The code is supposed to read in an integer and either cube it or square it depending on the button pressed. I was not getting an error before I put the logic in. Also the onOptionsItemSelected is giving me an error stating that it cannot resolve the method. Thanks in advance for help! If you notice error in the logic id appreciate tips as well.

package com.firstapp.conner.squareandcube;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;



public class MainActivity extends AppCompatActivity {

    private static int squared, cubed;

    Button square, cube;
    TextView tv, inputNum;
    int num1;
    String numString;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        square = (Button) findViewById(R.id.square_button);
        cube = (Button) findViewById(R.id.cube_button);

        inputNum = (TextView) findViewById(R.id.editText);
        inputNum.setText(inputNum.getText());
        num1 = Integer.parseInt(inputNum.toString());

        tv = (TextView) findViewById(R.id.textView);

        Button square = (Button)findViewById(R.id.square_button);

        square.setOnClickListener(new View.OnClickListener() {
            @Override
             public void onClick(View v) {

                squared = num1 * num1;

                numString = String.valueOf(squared);
                tv.setText(numString);

                cube.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        cubed = num1 * num1 * num1;

                        numString = String.valueOf(cubed);
                        tv.setText(numString);
                    }
                });


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
});}}
NaviRamyle
  • 3,967
  • 1
  • 31
  • 49
  • 1
    You need a `}` before the `onCreateOptionsMenu()` method. Actually, you've got several in the wrong places. Move `});}` from the end of the class to before `onCreateOptionsMenu()`. – Mike M. Jan 20 '16 at 03:15
  • What is the error string for overriding onCreate? – Pooya Jan 20 '16 at 03:15
  • method does not override from superclass – Conner Gesbocker Jan 20 '16 at 03:29
  • Okay thanks mike i am building successfully now – Conner Gesbocker Jan 20 '16 at 03:30
  • Actually just got another error, when i run my app it states, unfortunately square and cub has stopped. – Conner Gesbocker Jan 20 '16 at 03:32
  • Consult the following posts: http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this and http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors. – Mike M. Jan 20 '16 at 03:37
  • okay i have reviewed these and checked the log cat.. i am receiving a numberformatexception Is this how you go from edit text to an int? inputNum = (EditText) findViewById(R.id.editText); inputNum.setText(inputNum.getText()); int numInt = (int) num1.parseInt(inputNum.toString()); – Conner Gesbocker Jan 20 '16 at 03:58
  • Please try to search first for your errors and exceptions. http://stackoverflow.com/questions/11113238/getting-a-numberformatexception-from-a-numerical-edittext-field – Mike M. Jan 20 '16 at 04:06

1 Answers1

0

just copy and paste this code. there were braces errors so i resolved it for you.

package com.firstapp.conner.squareandcube;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;



public class MainActivity extends AppCompatActivity {

private static int squared, cubed;

Button square, cube;
TextView tv, inputNum;
int num1;
String numString;
setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    square = (Button) findViewById(R.id.square_button);
    cube = (Button) findViewById(R.id.cube_button);

    inputNum = (TextView) findViewById(R.id.editText);
    inputNum.setText(inputNum.getText());
    num1 = Integer.parseInt(inputNum.toString());

    tv = (TextView) findViewById(R.id.textView);

    Button square = (Button)findViewById(R.id.square_button);

    square.setOnClickListener(new View.OnClickListener() {
        @Override
         public void onClick(View v) {

            squared = num1 * num1;

            numString = String.valueOf(squared);
            tv.setText(numString);
        }
        });
    cube.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            cubed = num1 * num1 * num1;

            numString = String.valueOf(cubed);
            tv.setText(numString);
        }
    });

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}