我正在尝试制作一个相当简单的计算器应用程序,以熟悉android。我的目标是在单击xml布局文件上的模式按钮时加载具有trig,对数函数等的新接口。我也在清单中包含了活动,但是当我运行项目时它失败了。我能做些什么才能有效?
以下是主要活动类:
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);
}
}
}
这对我不起作用..我有什么办法可以解决它吗?
答案 0 :(得分:1)
要开始其他活动,您必须创建Intent intent = new Intent()
并向此意图指明您要使用setClass(Context packageContext, Class<?> cls)
方法开始的活动。
有不同的方法来构建一个intent,阅读文档。一切都在这里:
这是清理后代码的一部分,请尝试:
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{
private static EditText mShowResult;
private static Button mMode;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
mMode = (Button) findViewById(R.id.bMode);
mShowResult = (EditText) findViewById(R.id.result_id);
if (mMode != null) {
mMode.setOnClickListener(this);
}
}
@Override
public void onClick(View view) {
if(view == mMode) {
Intent intent = new Intent();
intent.setClass(this, SwitchMode.class);
startActivity(intent);
}
}
}
答案 1 :(得分:0)
最简单的解决方案是为Intent使用不同的构造函数:
更改
Intent i = new Intent("zero.paradox.valvemachine.SWITCHNODE");
到
Intent i = new Intent(getContext(), SwitchMode.class);
另外,你错误拼写了你的班级名称(切换* N * ode而不是switch * M * ode),这可能是你遇到问题的原因,你正在使用的构造函数也可以工作,我只想在String上使用强类型的类名。