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