2012-11-07 24 views
-2

我有兩個活動(主要和計算器)。當主按鈕被點擊,活動變爲計算器,但我不斷收到錯誤在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()); 

    } 
} 
同時運行
+2

什麼是錯誤?例如,發佈logcat。 – eightx2

+0

您可能在CalculatorActivity的佈局中存在XML通脹錯誤?需要stackTrace和更多詳細信息以幫助更多 –

+0

或者驗證您是否已將'CalculatorActivity'活動添加到'Manifest.xml'中 –

回答

0

同時創造意圖對象試試這個

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

並檢查是否CalculatorActivity未在清單文件中註冊