1

我是一个菜鸟,我使用搜索功能来获得答案,但我没有找到任何东西,所以我在 Android Studio 中有我的代码和我的 nexus 7 作为一个真实的设备来测试它,但每次我尝试运行它,我的关系上出现一条消息,说它已直接结束:S
通过单击按钮,数字被添加到 Textview,计算器的基本代码是我的代码:

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
int actuel;
Button btn0 = (Button) findViewById(R.id.btn0);
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
Button btn3 = (Button) findViewById(R.id.btn3);
Button btn4 = (Button) findViewById(R.id.btn4);
Button btn5 = (Button) findViewById(R.id.btn5);
Button btn6 = (Button) findViewById(R.id.btn6);
Button btn7 = (Button) findViewById(R.id.btn7);
Button btn8 = (Button) findViewById(R.id.btn8);
Button btn9 = (Button) findViewById(R.id.btn9);
TextView output = (TextView) findViewById(R.id.textView);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn0.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addNumbers(0,view);
        }
    });
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addNumbers(1,view);
        }
    });
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addNumbers(2,view);
        }
    });
    btn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addNumbers(3,view);
        }
    });
    btn4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addNumbers(4,view);
        }
    });
    btn5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addNumbers(5,view);
        }
    });
    btn6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addNumbers(6,view);
        }
    });
    btn7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addNumbers(7,view);
        }
    });
    btn8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addNumbers(8,view);
        }
    });
    btn9.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addNumbers(9,view);
        }
    });

}

private final void addNumbers(int num, View view){
    actuel = actuel * 10 + num;
    output.setText(String.valueOf(actuel));
}

}
4

1 回答 1

2

将您的代码更新为此:

 public class MainActivity extends Activity {
 int actuel;
 Button btn0 = null;
 Button btn1 = null;
 Button btn2 = null;
 Button btn3 = null;
 Button btn4 = null;
 Button btn5 = null;
 Button btn6 = null;
 Button btn7 = null;
 Button btn8 = null;
 Button btn9 = null;
 TextView output = null;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

      btn0 = (Button) findViewById(R.id.btn0);
      btn1 = (Button) findViewById(R.id.btn1);
      btn2 = (Button) findViewById(R.id.btn2);
      btn3 = (Button) findViewById(R.id.btn3);
      btn4 = (Button) findViewById(R.id.btn4);
      btn5 = (Button) findViewById(R.id.btn5);
      btn6 = (Button) findViewById(R.id.btn6);
      btn7 = (Button) findViewById(R.id.btn7);
      btn8 = (Button) findViewById(R.id.btn8);
      btn9 = (Button) findViewById(R.id.btn9);
      output = (TextView) findViewById(R.id.textView);
      ...

你的应用程序崩溃了,因为你所有的 btnX 和输出变量都是空的。

于 2013-06-23T00:04:25.833 回答