意图无法运行

时间:2012-11-07 23:02:14

标签: java android android-intent

我有两个活动(主要和计算器)。单击main中的按钮时,活动将更改为计算器,但在startActivity(意图)运行时仍然出现错误

MainActivity.java

public class MainActivity extends Activity {
    public final static String  EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    public int mult = 1;

    public int num1;
    public int num2;
    public int answer;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }



    public void calculator(View view)
    {
        Intent intent = new Intent(this, CalculatorActivity.class);
        startActivity(intent);
    }


}

CalculatorActivity.java

public class CalculatorActivity extends Activity
{
    Button b0; 
    Button b1; 
    Button b2; 
    Button b3; 
    Button b4; 
    Button b5; 
    Button b6; 
    Button b7; 
    Button b8; 
    Button b9; 
    Button bDot; 
    Button bNeg;
    Button bDiv; 
    Button bMult;
    Button bSub; 
    Button bPlus;
    Button bEnter;

    EditText edit;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);

        //Intent intent = getIntent();


        //Intent intent = getIntent();
        b0 = (Button) findViewById(R.id.button0);
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        b4 = (Button) findViewById(R.id.button4);
        b5 = (Button) findViewById(R.id.button5);
        b6 = (Button) findViewById(R.id.button6);
        b7 = (Button) findViewById(R.id.button7);
        b8 = (Button) findViewById(R.id.button8);
        b9 = (Button) findViewById(R.id.button9);
        bDot = (Button) findViewById(R.id.buttonDot);
        bNeg = (Button) findViewById(R.id.buttonNeg);
        bDiv = (Button) findViewById(R.id.buttonDiv);
        bMult = (Button) findViewById(R.id.buttonMult);
        bSub = (Button) findViewById(R.id.buttonMinus);
        bPlus = (Button) findViewById(R.id.buttonPlus);
        bEnter = (Button) findViewById(R.id.buttonEnter);

        edit= (EditText) findViewById(R.id.editText);
    }

    /** Called when the user clicks on a number */
    public void enterNum(View view)
    {
        b0.setOnClickListener(new OnClickListener()
        {
            public void onClick(View view)
            {
                edit.setText(b0.getText().toString());
            }
        });

        if(b0.isPressed())
            edit.setText( b0.getText().toString() );

    }
}

1 个答案:

答案 0 :(得分:0)

在创建intent对象时尝试这个

intent=new Intent(MainActivity.this, CalculatorActivity.class);

并检查CalculatorActivity是否已在清单文件中注册