在android中设置OnClickListener时出错
我是android的初学者,我编写了以下简单程序,只显示我在文本视图上按下的按钮的文本(我总是遇到运行时错误):在android中设置OnClickListener时出错,android,Android,我是android的初学者,我编写了以下简单程序,只显示我在文本视图上按下的按钮的文本(我总是遇到运行时错误): 您的btnPlus对象为空。您需要在onCreate方法中实例化它,如 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.app_page); btnPlus = (B
您的btnPlus对象为空。您需要在onCreate方法中实例化它,如
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_page);
btnPlus = (Button)findViewById(R.id.YOUR_BUTTON_ID);
btnMinus = (Button)findViewById(R.id.YOUR_BUTTON_ID);
btnMul = (Button)findViewById(R.id.YOUR_BUTTON_ID);
btnDiv = (Button)findViewById(R.id.YOUR_BUTTON_ID);
btnPlus.setOnClickListener(operations);
btnMinus.setOnClickListener(operations);
btnMul.setOnClickListener(operations);
btnDiv.setOnClickListener(operations);
}
在使用setText方法之前,还要实例化resultTextView对象。您的btnPlus对象为空。您需要在onCreate方法中实例化它,如
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_page);
btnPlus = (Button)findViewById(R.id.YOUR_BUTTON_ID);
btnMinus = (Button)findViewById(R.id.YOUR_BUTTON_ID);
btnMul = (Button)findViewById(R.id.YOUR_BUTTON_ID);
btnDiv = (Button)findViewById(R.id.YOUR_BUTTON_ID);
btnPlus.setOnClickListener(operations);
btnMinus.setOnClickListener(operations);
btnMul.setOnClickListener(operations);
btnDiv.setOnClickListener(operations);
}
在使用setText方法之前,还要实例化resultTextView对象。从代码中可以看出,问题在于您似乎没有初始化按钮(btnPlus、btnMinus、btnMul、btnDiv)和TextView resultTextView
btnPlus = (Button)findViewById(Button Id);
btnMinus = (Button)findViewById(Button Id);
btnMul = (Button)findViewById(Button Id);
btnDiv = (Button)findViewById(Button Id);
resultTextView = (Button)findViewById(TextView Id);
首先初始化这些视图,然后您可以对这些视图应用任何操作,如单击事件和设置文本。从代码中可以看出,问题在于您似乎没有初始化按钮(btnPlus、btnMinus、btnMul、btnDiv)和TextView resultTextView
btnPlus = (Button)findViewById(Button Id);
btnMinus = (Button)findViewById(Button Id);
btnMul = (Button)findViewById(Button Id);
btnDiv = (Button)findViewById(Button Id);
resultTextView = (Button)findViewById(TextView Id);
首先初始化这些视图,然后可以对这些视图应用任何操作,如单击事件和设置文本