Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8963051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:16:28+00:00 2026-06-15T16:16:28+00:00

i’am beginner and i’am trying to make a simple calculator for android but he

  • 0

i’am beginner and i’am trying to make a simple calculator for android but he give me a syntax error

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    final EditText e  = (EditText)findViewById(R.id.value);
    final EditText e2 = (EditText)findViewById(R.id.value2);

    getMenuInflater().inflate(R.menu.activity_main, menu);
    Toast.makeText(this,"Welcome",Toast.LENGTH_LONG).show();
    Button welcome = (Button)findViewById(R.id.x);
    welcome.setText("push ");
    welcome.setonclickListener(new View.onclickListener() {

        @Override
        public void onclick(View v) {
            int v1 = Integer.parseInt(e.getText().toString());
            int v2 = Integer.parseInt(e2.getText().toString());
            int v3 = v1 + v2 ;
            Toast.makeText(MainActivity.this,"= " + v3, Toast.LENGTH_LONG).show();

             return true;

         }
         });

}

}

http://imageshack.us/photo/my-images/24/problem00.png/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:16:29+00:00Added an answer on June 15, 2026 at 4:16 pm

    Use OnClickListener(). You don’t have to specify View. Also, make sure you use a capital ‘C’ in the overridden onClick() method. Make sure you include import android.view.View.OnClickListener; in your imports section. Eclipse should this for you, but if it’s not or you are using Eclipse, add it manually to the top of the class. If you simply do the following, that should work for you:

         welcome.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                int v1 = Integer.parseInt(e.getText().toString());
                int v2 = Integer.parseInt(e2.getText().toString());
                int v3 = v1 + v2;
                Toast.makeText(MainActivity.this, "= " + v3, Toast.LENGTH_LONG).show();
    
                return true;
    
            }
        });
    

    ——–EDITED FOR FULL EXAMPLE——–

    In your project, you have your main activity. That activity should use a layout from an XML resource. That XML resource likely looks something like this:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" />
    
        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>
    

    In your activity class, you have to set that resource as the content view for the activity, using setContentView(resID). Say your XML file is called helloworld.xml, you would execute setContentView(R.layout.helloworld) in the activity’s onCreate(Bundle s) method right after calling super.onCreate(s).

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.helloworld);
    
    }
    

    Once you have set the activity’s view, you can access that layout’s elements (EditTexts, Buttons, etc). To do this, you’ll have to create EditText and Button objects (which you were already doing in your posted code, we just have to do them elsewhere instead). Continuing with my example, you could do the following in your onCreate(Bundle s) function:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.helloworld);
    
        EditText et1 = (EditText) findViewById(R.id.edittext1);
        EditText et2 = (EditText) findViewById(R.id.edittext2);
        Button but = (Button) findViewById(R.id.button1);
    
        but.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v)
                et2.setText(et1.getText().toString());
                // this will set the second EditText's text to whatever is in
                // the first EditText, but you could do anything with the value.
            }
        }
    }
    

    If you want to change what the button does based on a menu item selection, you have to override onOptionsItemSelected(MenuItem item) in addition to onCreateOptionsMenu(Menu menu). onCreateOptionsMenu(Menu menu) simply creates the menu for the menu button to open. onOptionsItemSelected(MenuItem item) actually decides what to do when you select a menu item.

    See the tutorial at this page for full rundown http://developer.android.com/guide/topics/ui/menus.html, but here are their examples with some explanation. These examples are not from my example app above but from the Android Developer API pages. I strongly recommend you go over their tutorial.

    All you have to do in onCreateOptionsMenu(Menu menu) is tell Android where to get the menu and to inflate that resource. This means there is an XML file called game_menu in the menu folder of the res folder in your project’s directory.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.game_menu, menu);
        return true;
    }
    

    onOptionsItemSelected(MenuItem item) is where the meat of the menu logic goes, letting you perform different tasks based on which menu item get’s selected. In this example, the game_menu XML file mentioned in the above function has menu items called new_game and help.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.new_game:
                newGame();
                return true;
            case R.id.help:
                showHelp();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.