大神求指点

在模拟机上运行崩溃

我做的是一个简单的计算器,在模拟机上运行就崩溃,求大神指点;

这个是我的Layout布局文件的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.activitytest2.JsqActivity2">
    <!--显示器-->
    <EditText
        android:id="@+id/jsj_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
<!--第一行代码-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"

        android:weightSum="1">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_weight="0.25"
            android:text="1"
            android:textSize="17sp"
            android:id="@+id/jsj_1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="2"
            android:textSize="17sp"
            android:id="@+id/jsj_2"
            android:layout_weight="0.25" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/jsj_3"
            android:text="3"
            android:textSize="17sp"
            android:layout_weight="0.25"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#11cf53"
            android:id="@+id/jsj_plus"
            android:textSize="17sp"
            android:layout_weight="0.25"
            android:text="+"/>
    </LinearLayout>
    <!--第二行代码-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/jsj_4"
            android:text="4"
            android:textSize="17sp"
            android:layout_weight="0.25"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/jsj_5"
            android:text="5"
            android:textSize="17sp"
            android:layout_weight="0.25"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/jsj_6"
            android:text="6"
            android:textSize="17sp"
            android:layout_weight="0.25"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/jsj_jian"
            android:background="#2a68a6"
            android:textSize="17sp"
            android:text="-"
            android:layout_weight="0.25"/>
    </LinearLayout>
    <!--第三行代码-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1"
        android:orientation="horizontal">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/jsj_7"
            android:text="7"
            android:textSize="17sp"
            android:layout_weight="0.25"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/jsj_8"
            android:text="8"
            android:textSize="17sp"
            android:layout_weight="0.25"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/jsj_9"
            android:text="9"
            android:textSize="17sp"
            android:layout_weight="0.25"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/jsj_chen"
            android:background="#e33bc9"
            android:textSize="17sp"
            android:text="*"
            android:layout_weight="0.25"/>
    </LinearLayout>
    <!--第四行代码-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1"
        android:orientation="horizontal">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/jsj_clear"
            android:text="C"
            android:textSize="17sp"
            android:layout_weight="0.25"/>

        <Button
            android:id="@+id/jsj_0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:text="0"
            android:textSize="17sp" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/jsj_equal"
            android:text="="
            android:textSize="17sp"
            android:layout_weight="0.25"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/jsj_chu"
            android:background="#c30fa8"
            android:text="/"
            android:textSize="17sp"
            android:layout_weight="0.25"/>
    </LinearLayout>
</LinearLayout>

这个是我的activity2.java文件的代码



package com.example.activitytest2;

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

public class JsqActivity2 extends AppCompatActivity implements View.OnClickListener,View.OnLongClickListener{
    Button jsj_0;
    Button jsj_1;
    Button jsj_2;
    Button jsj_3;
    Button jsj_4;
    Button jsj_5;
    Button jsj_6;
    Button jsj_7;
    Button jsj_8;
    Button jsj_9;
    Button jsj_clear;
    Button jsj_equal;
    Button jsj_plus;
    Button jsj_jian;
    Button jsj_chen;
    Button jsj_chu;
    EditText jsj_input;
    boolean clear_flag ;//清空标识
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jsq2_layout);
        //        实例化各个按钮
        jsj_0=(Button)findViewById(R.id.jsj_0);
        jsj_1=(Button)findViewById(R.id.jsj_1);
        jsj_2=(Button)findViewById(R.id.jsj_2);
        jsj_3=(Button)findViewById(R.id.jsj_3);
        jsj_4=(Button)findViewById(R.id.jsj_4);
        jsj_5=(Button)findViewById(R.id.jsj_5);
        jsj_6=(Button)findViewById(R.id.jsj_6);
        jsj_7=(Button)findViewById(R.id.jsj_7);
        jsj_8=(Button)findViewById(R.id.jsj_9);
        jsj_equal=(Button)findViewById(R.id.jsj_equal);
        jsj_chen=(Button)findViewById(R.id.jsj_chen);
        jsj_chu=(Button)findViewById(R.id.jsj_chu);
        jsj_jian=(Button)findViewById(R.id.jsj_jian);
        jsj_plus=(Button)findViewById(R.id.jsj_plus);
        jsj_input=(EditText)findViewById(R.id.jsj_input);
//        设置按钮监听事件
        jsj_0.setOnClickListener(this);
        jsj_1.setOnClickListener(this);
        jsj_2.setOnClickListener(this);
        jsj_3.setOnClickListener(this);
        jsj_4.setOnClickListener(this);
        jsj_5.setOnClickListener(this);
        jsj_6.setOnClickListener(this);
        jsj_7.setOnClickListener(this);
        jsj_8.setOnClickListener(this);
        jsj_clear.setOnClickListener(this);
        jsj_input.setOnClickListener(this);
        jsj_plus.setOnClickListener(this);
        jsj_jian.setOnClickListener(this);
        jsj_chen.setOnClickListener(this);
        jsj_chu.setOnClickListener(this);
        jsj_equal.setOnClickListener(this);

    }

    @Override
    public boolean onLongClick(View v) {

        return false;
    }

    @Override
    public void onClick(View v) {
    String str=jsj_input.getText().toString();
        switch (v.getId()){
            case R.id.jsj_0:
            case R.id.jsj_1:
            case R.id.jsj_2:
            case R.id.jsj_3:
            case R.id.jsj_4:
            case R.id.jsj_5:
            case R.id.jsj_6:
            case R.id.jsj_7:
            case R.id.jsj_8:
            case R.id.jsj_9:
// 如果当前显示的有结果,即 clear_flag==true,那么下一次你点击的时候,首先要将当前结果清空掉,即,将 clear_flag设置为 false。
                if (clear_flag){
                    clear_flag=true;
                    str="";
                    jsj_input.setText(" ");
                }
//                将视图对象的text值,输出到jsj_input中显示出来
                jsj_input.setText(str+((Button)v).getText());
                break;
            case R.id.jsj_plus:
            case R.id.jsj_jian:
            case R.id.jsj_chen:
            case R.id.jsj_chu:
                if (clear_flag){
                    clear_flag=true;
                    str="";
                    jsj_input.setText(" ");
                }
                jsj_input.setText(str+" "+((Button)v).getText()+" ");
                break;
            case R.id.jsj_clear:
                clear_flag=false;
                str=" ";
                jsj_input.setText("");
            case R.id.jsj_equal:
                getResult();
                break;

        }
    }

    private void getResult(){
    String zhi=jsj_input.getText().toString();
        if(zhi.equals("")||zhi==null){
            return;
        }
        if(clear_flag){
            clear_flag=false;
            return;
        }
        /*这个方法是描述得到的字符串是否包含某个字符串,例a.contains("123"),就是a里面是否包含123,包含为true,否则为false*/
        if(!zhi.contains("")){
            return;
        }
        if(clear_flag){
            clear_flag=false;
            return;
        }
        clear_flag=true;
        double s =0;
        String s1=zhi.substring(0,zhi.indexOf(" "));
        String c=zhi.substring(zhi.indexOf(" ")+1,zhi.indexOf(" ")+2);
        String s2=zhi.substring(zhi.indexOf(" ")+2);
        double d1=Double.parseDouble(s1);
        double d2=Double.parseDouble(s2);
        if (c=="+"){
            s=d1+d2;
        }else if (c=="*"){
            s=d1*d2;
        }else  if(c=="-"){
            s=d1-d2;
        }else  if(c=="/"){
            s=d1/d2;
        }
        int r;
        r=(int)s; 
        jsj_input.setText(r);

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值