I am trying to make a fairly simple calculator app for getting familiar with android. My goal is to load a new interface with trig, logarithmic functions, etc. upon clicking the mode button on the xml layout file. I included activity in the manifest as well but it failed when I ran the project. What can I do so that it works?
Here is the main activity class:
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);
}
}
And the code for the new mode:
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);
}
}
}
This is not working for me.. is there anything I can do to fix it?
In order to starting another activity you have to create a Intent intent = new Intent()
and indicate to this intent the activity you want to start with setClass(Context packageContext, Class<?> cls)
method.
There are different ways to construct a intent, read the documentation. Everything is here :
This is a part of your code after a clean up, try this :
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);
}
}
}
The simplest solution is to use a different constructor for your Intents:
Change
Intent i = new Intent("zero.paradox.valvemachine.SWITCHNODE");
to
Intent i = new Intent(getContext(), SwitchMode.class);
Also, you misspelled your class name (switch* N *ode instead of switch* M *ode), which is probably why you are having the problem, as the constructor you are using should also work, I just prefer to use strongly typed class names over a String.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.