我正在尝试制作一个相当简单的计算器应用程序来熟悉 android。我的目标是在单击 xml 布局文件上的模式按钮时加载一个带有三角函数、对数函数等的新界面。我也在清单中包含了活动,但是当我运行项目时它失败了。我该怎么做才能使它起作用?
这是主要的活动类:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Calculator extends Activity implements OnClickListener{
public String str = "";
Character task = 'q';
double num, numtemp;
EditText showResult;
Button mode;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
showResult = (EditText) findViewById(R.id.result_id);
mode = (Button) findViewById(R.id.bMode);
mode.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.bMode)
{
Intent i = new Intent("zero.paradox.valvemachine.SWITCHNODE");
startActivity(i);
}
}
public void btn1Clicked(View v) {
insert(1);
}
public void btn2Clicked(View v) {
insert(2);
}
public void btn3Clicked(View v) {
insert(3);
}
public void btn4Clicked(View v) {
insert(4);
}
public void btn5Clicked(View v) {
insert(5);
}
public void btn6Clicked(View v) {
insert(6);
}
public void btn7Clicked(View v) {
insert(7);
}
public void btn8Clicked(View v) {
insert(8);
}
public void btn9Clicked(View v) {
insert(9);
}
public void btn0Clicked(View v){
insert(0);
}
public void btnplusClicked(View v) {
perform();
task = '+';
}
public void btnminusClicked(View v) {
perform();
task = '-';
}
public void btndivideClicked(View v) {
perform();
task = '/';
}
public void btnmultiClicked(View v) {
perform();
task = '*';
}
public void btnequalClicked(View v) {
calculate();
}
public void btnclearClicked(View v) {
reset();
}
private void reset() {
// TODO Auto-generated method stub
str = "";
task = 'q';
num = 0;
numtemp = 0;
showResult.setText("");
}
private void insert(int j) {
// TODO Auto-generated method stub
str = str + Integer.toString(j);
num = Integer.valueOf(str).intValue();
showResult.setText(str);
}
private void perform() {
// TODO Auto-generated method stub
str = "";
numtemp = num;
}
private void calculate() {
// TODO Auto-generated method stub
if (task == '+')
num = numtemp + num;
else if (task == '-')
num = numtemp - num;
else if (task == '/')
num = numtemp / num;
else if (task == '*')
num = numtemp * num;
showResult.setText("" + num);
}
}
以及新模式的代码:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class SwitchMode extends Activity implements OnClickListener{
Button mode2;
EditText showResult2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display2);
showResult2 = (EditText) findViewById(R.id.edtNode);
mode2 = (Button) findViewById(R.id.bMode);
mode2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.mode)
{
Intent i = new Intent("org.calculator.MAINACTIVITY");
startActivity(i);
}
}
}
这对我不起作用..我能做些什么来解决它吗?