2015-06-06 181 views
0

使用Android Studio,您能指出錯誤嗎?我無法分析爲什麼我不得不面對這個錯誤: 只有java的基本背景,這就是我已經創建了計算方法。獲取應用程序停止錯誤

package com.tlians.t_calculator; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 


public class MainActivity extends ActionBarActivity { 


Button add=(Button) findViewById(R.id.add); 
Button sub=(Button) findViewById(R.id.sub); 
Button times=(Button) findViewById(R.id.times); 
Button div=(Button) findViewById(R.id.div); 
EditText num1=(EditText) findViewById(R.id.num1); 
EditText num2=(EditText) findViewById(R.id.num2); 
TextView result=(TextView) findViewById(R.id.result); 


public void calculate(char a){ 
    String N1=num1.getText().toString(); 
    String N2=num2.getText().toString(); 
    float n1=Float.parseFloat(N1); 
    float n2=Float.parseFloat(N2); 
    if(a=='+') 
     result.setText(""+n1+n2); 
    else if(a=='-') 
     result.setText(""+(n1-n2)); 
    else if(a=='/') 
     result.setText(""+n1/n2); 
    else if(a=='*') 
     result.setText(""+n1*n2); 
    else 
     result.setText("Tani has only assigned four functions right now."); 

} 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

     add.setOnClickListener(new Button.OnClickListener(){ 
      public void onClick(View v){ 
       calculate('+'); 
      } 
     }); 
     sub.setOnClickListener(new Button.OnClickListener(){ 
      public void onClick(View v){ 
       calculate('-'); 
      } 

     }); 
     times.setOnClickListener(new Button.OnClickListener(){ 
      public void onClick(View v){ 
       calculate('*'); 
      } 
     }); 
     div.setOnClickListener(new Button.OnClickListener(){ 
      public void onClick(View v){ 
       calculate('/'); 
      } 
     }); 


} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 
+2

檢查logcat的輸出爲崩潰的軟件除外。當你找到它時,點擊*編輯*並添加堆棧跟蹤到你的問題。確定什麼是錯的,沒有真正的錯誤信息是困難的... – trooper

回答

1
Button add; 
Button sub; 
Button times; 
Button div; 
EditText num1; 
EditText num2; 
TextView result; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

Button add=(Button) findViewById(R.id.add); 
sub=(Button) findViewById(R.id.sub); 
times=(Button) findViewById(R.id.times); 
div=(Button) findViewById(R.id.div); 
num1=(EditText) findViewById(R.id.num1); 
num2=(EditText) findViewById(R.id.num2); 
result=(TextView) findViewById(R.id.result); 

     add.setOnClickListener(new Button.OnClickListener(){ 
      public void onClick(View v){ 
       calculate('+'); 
      } 
     }); 
     sub.setOnClickListener(new Button.OnClickListener(){ 
      public void onClick(View v){ 
       calculate('-'); 
      } 

     }); 
     times.setOnClickListener(new Button.OnClickListener(){ 
      public void onClick(View v){ 
       calculate('*'); 
      } 
     }); 
     div.setOnClickListener(new Button.OnClickListener(){ 
      public void onClick(View v){ 
       calculate('/'); 
      } 
     }); 


} 
+0

比光更快! –

+1

@DerGolem真的,但你的答案其實更好。 – trooper

+0

呃...我不想每個問題都增加重複的答案。一個就足夠了。你先於我,你是榮譽!讓我們成爲運動員吧。 –

0

初始化內部onCreate()方法所有按鈕和的EditText。你可以聲明按鈕,edittext任何組件到全局,但不要將它們初始化爲全局。所以這樣做:

Button add; 
Button sub; 
Button times; 
Button div; 
EditText num1; 
EditText num2; 
TextView result; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

add=(Button) findViewById(R.id.add); 
sub=(Button) findViewById(R.id.sub); 
times=(Button) findViewById(R.id.times); 
div=(Button) findViewById(R.id.div); 
num1=(EditText) findViewById(R.id.num1); 
num2=(EditText) findViewById(R.id.num2); 
result=(TextView) findViewById(R.id.result); 

. 
. 
. 
//your button listener code 

} 
相關問題