在 android 中创建计算器菜单

Creating calculator menu in android

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

我输入第一个和第二个数字,然后选择是否要对这两个数字进行加、减、除、乘或乘以 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

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

最初文本视图中没有任何值。从 setContentView 方法中删除这些行。在计算结果之前在 OnOptionsItemSelected 函数中添加这些行。

希望对您有所帮助。

textview text1 & text2 的值是空的,这就是它崩溃的原因 删除 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);

    }

并且由于您声明了 fNum & sNum 一个全局变量。我们可以创建一个函数并从 text1 & text2

中获取值
// 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);
}