有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android应用程序过早退出

我是使用JAVA开发安卓的新手,我对自己创建的一个简单应用程序有意见。(请不要嘲笑它!)

它所要做的就是在可编辑的文本视图中显示一个数字;此主要活动上的三个按钮是+、-和重置。超级简单,对吧?我不知道我做错了什么,但每次我运行应用程序进行测试,然后单击任何按钮,它就会退出应用程序并返回安卓主屏幕。不知道我做错了什么。。。但这是我目前掌握的代码:

    public class Main extends Activity {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    Button btn_Plus;
    Button btn_Minus;
    Button btn_Reset;
    final EditText sCount= (EditText) findViewById(R.id.txtCount);;


    btn_Plus=(Button)findViewById(R.id.btnPlus);
    btn_Minus=(Button)findViewById(R.id.btnMinus);
    btn_Reset=(Button)findViewById(R.id.btnReset);

            //One way I tried to work it
        btn_Plus.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                                    //gets the text in the textview, makes it a string,
                                    //converts it to an int, calculates, then puts it 
                                    //back. 

                String iCounter = sCount.getText().toString();
                int iCount = Integer.parseInt(iCounter);
                iCount += 1;
                Integer.toString(iCount);
                sCount.setText(iCount);
            }   
    });

                    //The second way I tried -- neither way works.
        bM.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

            int iCount = Integer.parseInt(sCount.getText().toString());
                iCount -= 1;
                    if(iCount >0)
                        iCount = 0;
                Integer.toString(iCount);
                sCount.setText(iCount);
            }   
        });

        bR.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                int iCount = 0;
                Integer.toString(iCount);
                sCount.setText(iCount);


            }

        });


    }

    }

非常感谢


共 (1) 个答案

  1. # 1 楼答案

    请改用文本视图,除非您计划允许用户直接输入号码(您似乎没有这样做)。与其通过文本操作来获取数字,不如将数字存储为成员变量,在按下按钮时进行更新,并相应地更改文本?我会这样写:

    public class Main extends Activity {
    
        int count = 0;
        TextView textView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            textView = (TextView) findViewById(R.id.txtCount);
            findViewById(R.id.btnPlus).setOnClickListener(this);
            findViewById(R.id.btnMinus).setOnClickListener(this);
            findViewById(R.id.btnReset).setOnClickListener(this);
        }
    
        private void updateText() {
            String text = Integer.toString(count);
            textView.setText(text);
        }
    
        @Override
        public void onClick(View v) {
            switch(v.getId()) {
            case R.id.btnPlus:
                count++;
                break;
            case R.id.btnMinus:
                count ;
                break;
            case R.id.btnReset:
                count = 0;
                break;
            }
    
            updateText();
        }
    }