Android Studio:应用程序崩溃

时间:2015-10-19 12:15:47

标签: android android-studio

我在Android Studio上创建了一个计算器应用程序并成功创建了它。我在手机上安装后,应用程序甚至没有启动它立即崩溃..

我找不到任何答案所以请帮忙......

这是我的代码:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnAdd,btnSub,btnMul,btnDiv;
private TextView rRes;
private EditText txtOne,txtTwo;

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

private void init() {
    btnAdd = (Button) findViewById(R.id.btnAdd);
    btnSub = (Button) findViewById(R.id.btnSub);
    btnMul = (Button) findViewById(R.id.btnMul);
    btnDiv = (Button) findViewById(R.id.btnDiv);
    txtOne = (EditText) findViewById(R.id.txtOne);
    txtTwo = (EditText) findViewById(R.id.txtTwo);
    rRes = (TextView) findViewById(R.id.rRes);

    btnAdd.setOnClickListener(this);
    btnSub.setOnClickListener(this);
    btnMul.setOnClickListener(this);
    btnDiv.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    String num1 = txtOne.getText().toString();
    String num2 = txtTwo.getText().toString();
    switch(v.getId()){
        case R.id.btnAdd:
            int add = Integer.parseInt(num1) + Integer.parseInt(num2);
            rRes.setText(String.valueOf(add));
            break;
        case R.id.btnSub:
            int sub = Integer.parseInt(num1) - Integer.parseInt(num2);
            rRes.setText(String.valueOf(sub));
            break;
        case R.id.btnMul:
            int mul = Integer.parseInt(num1) * Integer.parseInt(num2);
            rRes.setText(String.valueOf(mul));
            break;
        case R.id.btnDiv:
            try{
                double div = Double.parseDouble(num1) / Double.parseDouble(num2);
                rRes.setText(String.valueOf(div));
            }catch(Exception e){
                rRes.setText("Not Divisible!!");
            }
            break;
    }

}

这是清单:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

0 个答案:

没有答案