在Android中创建计算器菜单

时间:2018-11-07 14:11:24

标签: java android android-studio menu option

我想通过菜单创建一个计算器。

我输入第一个和第二个数字,然后选择是否要对两个数字进行加,减,除,乘,乘运算或乘以3或4。

也可以使用选项菜单和上下文菜单。 这是我到目前为止所做的。

package com.example.asus.menuassignment;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


    EditText text1;
    EditText text2;
    TextView text;
    int fNum;
    int sNum;
    int result;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text1 = (EditText)findViewById(R.id.editText);
        text2 = (EditText)findViewById(R.id.editText2);
        text = (TextView)findViewById(R.id.textView3);
        fNum = Integer.parseInt(text1.getText().toString());
        sNum = Integer.parseInt(text2.getText().toString());
        registerForContextMenu(text);

    }
//options menu part
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {

        switch (item.getItemId())
        {
            //multiply

            case R.id.item1:
                result = fNum*sNum;
                text.setText(String.valueOf(result));
                return true;

            //multiplying a result by 3 or 4
            case R.id.item11:
                result = result * 3;
                text.setText(String.valueOf(result));
                return true;
            case R.id.item12:
                result = result * 4;
                text.setText(String.valueOf(result));
                return true;

                //Add
            case R.id.item2:
                result = fNum + sNum;
                text.setText(String.valueOf(result));
                return true;
                //Subtract
            case R.id.item3:
                result = fNum - sNum;
                text.setText(String.valueOf(result));
                return true;
                //Divide
            case R.id.item4:
                result = fNum / sNum;
                text.setText(String.valueOf(result));
                return true;


        }
        return super.onOptionsItemSelected(item);
    }
 //By long pressing the result a context menu appears
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
    {
        getMenuInflater().inflate(R.menu.menu, menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
        switch (item.getItemId()) {
            //multiply

            case R.id.item1:
                result = fNum * sNum;
                text.setText(String.valueOf(result));
                return true;

            //multiplying a result by 3 or 4
            case R.id.item11:
                result = result * 3;
                text.setText(String.valueOf(result));
                return true;
            case R.id.item12:
                result = result * 4;
                text.setText(String.valueOf(result));
                return true;

            //Add
            case R.id.item2:
                result = fNum + sNum;
                text.setText(String.valueOf(result));
                return true;
            //Subtract
            case R.id.item3:
                result = fNum - sNum;
                text.setText(String.valueOf(result));
                return true;
            //Divide
            case R.id.item4:
                result = fNum / sNum;
                text.setText(String.valueOf(result));
                return true;
        }

        return super.onContextItemSelected(item);
    }

}

我没有任何错误,但是应用程序崩溃了,究竟是什么错误或这里缺少什么?

更新:This is the logcat

2 个答案:

答案 0 :(得分:0)

该应用程序崩溃,原因是以下代码 fNum = Integer.parseInt(text1.getText()。toString()); sNum = Integer.parseInt(text2.getText()。toString());

最初,textview中将没有任何值。从setContentView方法中删除这些行。在计算结果之前,将这些行添加到OnOptionsItemSelected函数中。

希望有帮助。

答案 1 :(得分:0)

textview text1text2的值为null,这就是它崩溃的原因 删除onCreate上的初始化

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text1 = (EditText)findViewById(R.id.editText);
        text2 = (EditText)findViewById(R.id.editText2);
        text = (TextView)findViewById(R.id.textView3);        
        // fNum = Integer.parseInt(text1.getText().toString()); // remove this line        
        // sNum = Integer.parseInt(text2.getText().toString()); // remove this line
        registerForContextMenu(text);

    }

并且由于您声明fNumsNum是全局变量。我们可以创建一个函数并从text1text2

获取值
// Get the data from text view
private void getData() { 
   fNum = Integer.parseInt(text1.getText() + ""); 
   sNum = Integer.parseInt(text2.getText() + ""); 
}

在执行计算之前先调用它

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    getData() // GET THE DATA

    switch (item.getItemId()) {
        case R.id.item1:
            result = fNum*sNum;
            text.setText(String.valueOf(result));
            return true;
        .
        .
        .
    }
    return super.onOptionsItemSelected(item);
}

以及此处:

@Override
public boolean onContextItemSelected(MenuItem item) {

    getData() // GET THE DATA

    switch (item.getItemId()) {
        case R.id.item1:
            result = fNum * sNum;
            text.setText(String.valueOf(result));
            return true;
        .
        .
        .
    return super.onContextItemSelected(item);
}