第一次做Android App(手机计算器),我遇到的错误

第一次做软件,脑子比较愚钝,犯过不少错误,都说吃一堑长一智,有些错误让我明了一些道理,但有些错误,犯了也不知道哪里错了,该怎么改,我就换了另一种方法解决问题,这反倒是吃一堑躲一堑了,固然不好。这篇日志,写我这次遇到的错误,希望看到的大神们给我指点指点,就算我得不到指点,我仍然希望把它们记录下来,等到我成长了,再回头看,相信那时更加强大的我能够解决它们,总之我打死不会放过它们的!

先说layout吧(UI界面的布局),这个是一定没错的

首先,是layout的标题:top.xml(待会被main.xml给include)


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/top"

    >
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/background1"
        android:text="策划:我;美工:我;编程:我;版权所有:我;配音:我和Tom"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">TextView>

RelativeLayout>

然后是main.xml

"http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main"
    >
    "@layout/top" android:id="@+id/top">

    id="@+id/body"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background"
        android:layout_below="@+id/top"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">


        id="@+id/equal"
            android:src="@drawable/equal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
        id="@+id/zero"
            android:src="@drawable/zero"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/equal"
            android:layout_alignTop="@id/equal"
            />
        id="@+id/point"
            android:src="@drawable/point"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/zero"
            android:layout_alignTop="@id/zero"
            />
        id="@+id/divide"
            android:src="@drawable/divide"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/equal"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            />
        id="@+id/nine"
            android:src="@drawable/nine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/divide"
            android:layout_toLeftOf="@+id/divide"
            android:layout_toStartOf="@+id/divide" />

        id="@+id/six"
            android:src="@drawable/six"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/nine"
            android:layout_toLeftOf="@+id/nine"
            android:layout_toStartOf="@+id/nine"
            />
        id="@+id/three"
            android:src="@drawable/three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/six"
            android:layout_toLeftOf="@+id/six"
            android:layout_toStartOf="@+id/six"
            />
        id="@+id/multiply"
            android:src="@drawable/multiply"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/divide"
            android:layout_alignParentRight="true"
            />
        id="@+id/eight"
            android:src="@drawable/eight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/multiply"
            android:layout_toLeftOf="@+id/multiply"
            android:layout_toStartOf="@+id/multiply"
            />
        id="@+id/five"
            android:src="@drawable/five"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/eight"
            android:layout_toLeftOf="@+id/eight"
            android:layout_toStartOf="@+id/eight"
            />
        id="@+id/two"
            android:src="@drawable/two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/five"
            android:layout_toLeftOf="@+id/five"
            android:layout_toStartOf="@+id/five"
            />
        id="@+id/less"
            android:src="@drawable/less"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/multiply"
            android:layout_alignParentRight="true"
            />
        id="@+id/seven"
            android:src="@drawable/seven"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/less"
            android:layout_toLeftOf="@+id/less"
            android:layout_toStartOf="@+id/less"
            />
        id="@+id/four"
            android:src="@drawable/four"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/seven"
            android:layout_toLeftOf="@+id/seven"
            android:layout_toStartOf="@+id/seven"
            />
        id="@+id/one"
            android:src="@drawable/one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/four"
            android:layout_toLeftOf="@+id/four"
            android:layout_toStartOf="@+id/four"
            />
        id="@+id/plus"
            android:src="@drawable/plus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/less"
            android:layout_alignParentRight="true"
            />
        id="@+id/jiong"
            android:src="@drawable/jiong"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/plus"
            android:layout_toLeftOf="@+id/plus"
            android:layout_toStartOf="@+id/plus"
            />

        "wrap_content"
            android:layout_width="fill_parent"
            android:id="@+id/printf1"
            android:textSize="25sp"
            android:layout_alignParentTop="true"
            android:text=""
            android:layout_alignRight="@+id/printf2"
            android:layout_alignEnd="@+id/printf2" />
        "fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/printf2"
            android:textSize="25dp"
            android:text=""
            android:layout_below="@+id/printf1"
            android:layout_alignRight="@+id/plus"
            android:layout_alignEnd="@+id/plus" />
        "fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/printf3"
            android:textSize="25dp"
            android:text=""
            android:layout_below="@+id/printf2"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
        "fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/printf4"
            android:textSize="25dp"
            android:text=""
            android:layout_below="@+id/printf3"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
    


再然后是我在main_activity的同一个package中写的一个工具类:MyClick.java(会在main_activity中用到,其作用如其名:printf)

package chenyuwei.com.aftermath;

import android.widget.TextView;

/**
 * Created by Administrator on 2015/2/10.
 */
public class MyClick
{
    String text;
    public void printf(TextView c, String now)
    {
        String pervious;
        pervious=(String)c.getText();
        text=pervious+now;
        c.setText(text);
    }
    public String mytext()
    {
        return text;
    }
}

再来个正确的main_activity

package chenyuwei.com.aftermath;

import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends Activity {
    private ImageButton Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Equal,Point,Delete,Plus,Less,Multiply,Divide;
    private TextView printf1,printf2,printf3,printf4;
    private MyClick My1=new MyClick();//第一个数字的TextView
    private MyClick My2=new MyClick();//加减乘除的TextView
    private MyClick My3=new MyClick();//第二个数字的TextView
    private MyClick My4=new MyClick();//等于和答案的TextView
    private int flag1=0;//判断加减乘除
    private int flag2=0;//判断是否给出计算答案
    private double result;//答案
    private static final int SRC_QUALITY = 0;
    private SoundPool soundPool = null;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Zero=(ImageButton)findViewById(R.id.zero);
        One=(ImageButton)findViewById(R.id.one);
        Two=(ImageButton)findViewById(R.id.two);
        Three=(ImageButton)findViewById(R.id.three);
        Four=(ImageButton)findViewById(R.id.four);
        Five=(ImageButton)findViewById(R.id.five);
        Six=(ImageButton)findViewById(R.id.six);
        Seven=(ImageButton)findViewById(R.id.seven);
        Eight=(ImageButton)findViewById(R.id.eight);
        Nine=(ImageButton)findViewById(R.id.nine);
        Equal=(ImageButton)findViewById(R.id.equal);
        Point=(ImageButton)findViewById(R.id.point);
        Delete=(ImageButton)findViewById(R.id.jiong);
        Plus=(ImageButton)findViewById(R.id.plus);
        Less=(ImageButton)findViewById(R.id.less);
        Multiply=(ImageButton)findViewById(R.id.multiply);
        Divide=(ImageButton)findViewById(R.id.divide);
        printf1=(TextView)findViewById(R.id.printf1);
        printf2=(TextView)findViewById(R.id.printf2);
        printf3=(TextView)findViewById(R.id.printf3);
        printf4=(TextView)findViewById(R.id.printf4);

        soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, SRC_QUALITY);
        final int[] sp=                                           //用来加载音频文件的数组(话说为什么大家都喜欢用hashmap?小白不明白)
                {
                        soundPool.load(this,R.raw.zero,1),//0
                        soundPool.load(this,R.raw.one,1),//1
                        soundPool.load(this,R.raw.two,1),//2
                        soundPool.load(this,R.raw.three,1),//3
                        soundPool.load(this,R.raw.four,1),//4
                        soundPool.load(this,R.raw.five,1),//5
                        soundPool.load(this,R.raw.six,1),//6
                        soundPool.load(this,R.raw.seven,1),//7
                        soundPool.load(this,R.raw.eight,1),//8
                        soundPool.load(this,R.raw.nine,1),//9
                        soundPool.load(this,R.raw.point,1),//10
                        soundPool.load(this,R.raw.plus,1),//11
                        soundPool.load(this,R.raw.less,1),//12
                        soundPool.load(this,R.raw.multiply,1),//13
                        soundPool.load(this,R.raw.divide,1),//14
                        soundPool.load(this,R.raw.equal,1),//15
                        soundPool.load(this,R.raw.jiong,1),//16
                        soundPool.load(this,R.raw.hundred,1),//17               result=100           flag2=1
                        soundPool.load(this,R.raw.bendan,1),//18                0<=x-y<=10           flag2=2
                        soundPool.load(this,R.raw.shabi,1),//19                 result=0             flag2=3
                        soundPool.load(this,R.raw.oneplusone,1),//20           1+1                  flag2=4
                        soundPool.load(this,R.raw.twomultiplytwo,1),//21      2*2                  flag2=5
                        soundPool.load(this,R.raw.ten,1),//22                   x+y<=10&&x!=1&&y!=1  flag2=6
                        soundPool.load(this,R.raw.nineninemultiply,1),//23    x*y&&0
                };

        Zero.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (flag1 ==0){My1.printf(printf1, "0");//flag1==0,那么0会输出到第一个数所在的TextView
                    soundPool.play(sp[0],1,1,0,0,1);}
                else if(flag1 ==-1)//         flag1=-1,也就是第一个数和第二个数与运算符号和答案都存在的情况,此时先清屏,再输出
                {
                    printf1.setText("");
                    printf2.setText("");
                    printf3.setText("");
                    printf4.setText("");
                    flag1 =flag2=0;
                    My1.printf(printf1,"0");
                }
                else {My3.printf(printf3,"0");soundPool.play(sp[0],1,1,0,0,1);}//0会输出到第二个数所在的TextView
            }
        });
        One.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view)
            {
                if (flag1 ==0){My1.printf(printf1, "1");soundPool.play(sp[1],1,1,0,0,1);}
                else if(flag1 ==-1)
                {
                    printf1.setText("");
                    printf2.setText("");
                    printf3.setText("");
                    printf4.setText("");
                    flag1 =flag2=0;
                    My1.printf(printf1,"1");
                }
                else My3.printf(printf3,"1");{soundPool.play(sp[1],1,1,0,0,1);}
            }
        });
        Two.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                if (flag1 ==0){My1.printf(printf1, "2");soundPool.play(sp[2],1,1,0,0,1);}
                else if(flag1 ==-1)
                {
                    printf1.setText("");
                    printf2.setText("");
                    printf3.setText("");
                    printf4.setText("");
                    flag1 =flag2=0;
                    My1.printf(printf1,"2");
                }
                else {My3.printf(printf3,"2");soundPool.play(sp[2],1,1,0,0,1);}
            }
        });
        Three.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                if (flag1 ==0){My1.printf(printf1, "3");soundPool.play(sp[3],1,1,0,0,1);}
                else if(flag1 ==-1)
                {
                    printf1.setText("");
                    printf2.setText("");
                    printf3.setText("");
                    printf4.setText("");
                    flag1 =flag2=0;
                    My1.printf(printf1,"3");
                }
                else{ My3.printf(printf3,"3");soundPool.play(sp[3],1,1,0,0,1);}
            }
        });
        Four.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (flag1 ==0){My1.printf(printf1, "4");soundPool.play(sp[4],1,1,0,0,1);}
                else if(flag1 ==-1)
                {
                    printf1.setText("");
                    printf2.setText("");
                    printf3.setText("");
                    printf4.setText("");
                    flag1 =flag2=0;
                    My1.printf(printf1,"4");
                }
                else {My3.printf(printf3,"4");soundPool.play(sp[4],1,1,0,0,1);}
            }
        });
        Five.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (flag1 ==0){My1.printf(printf1, "5");soundPool.play(sp[5],1,1,0,0,1);}
                else if(flag1 ==-1)
                {
                    printf1.setText("");
                    printf2.setText("");
                    printf3.setText("");
                    printf4.setText("");
                    flag1 =flag2=0;
                    My1.printf(printf1,"5");
                }
                else {My3.printf(printf3,"5");soundPool.play(sp[5],1,1,0,0,1);}
            }
        });
        Six.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (flag1 ==0){My1.printf(printf1, "6");soundPool.play(sp[6],1,1,0,0,1);}
                else if(flag1 ==-1)
                {
                    printf1.setText("");
                    printf2.setText("");
                    printf3.setText("");
                    printf4.setText("");
                    flag1 =flag2=0;
                    My1.printf(printf1,"6");
                }
                else {My3.printf(printf3,"6");soundPool.play(sp[6],1,1,0,0,1);}
            }
        });
        Seven.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (flag1 ==0){My1.printf(printf1, "7");soundPool.play(sp[7],1,1,0,0,1);}
                else if(flag1 ==-1)
                {
                    printf1.setText("");
                    printf2.setText("");
                    printf3.setText("");
                    printf4.setText("");
                    flag1 =flag2=0;
                    My1.printf(printf1,"7");
                }
                else {My3.printf(printf3,"7");soundPool.play(sp[7],1,1,0,0,1);}
            }
        });
        Eight.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (flag1 ==0){My1.printf(printf1, "8");soundPool.play(sp[8],1,1,0,0,1);}
                else if(flag1 ==-1)
                {
                    printf1.setText("");
                    printf2.setText("");
                    printf3.setText("");
                    printf4.setText("");
                    flag1 =flag2=0;
                    My1.printf(printf1,"8");
                }
                else {My3.printf(printf3,"8");soundPool.play(sp[8],1,1,0,0,1);}
            }
        });
        Nine.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (flag1 ==0){My1.printf(printf1, "9");soundPool.play(sp[9],1,1,0,0,1);}
                else if(flag1 ==-1)
                {
                    printf1.setText("");
                    printf2.setText("");
                    printf3.setText("");
                    printf4.setText("");
                    flag1 =flag2=0;
                    My1.printf(printf1,"9");
                }
                else {My3.printf(printf3,"9");soundPool.play(sp[9],1,1,0,0,1);}
            }
        });
        Point.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (flag1 ==0){My1.printf(printf1, ".");soundPool.play(sp[10],1,1,0,0,1);}
                else if(flag1 ==-1)
                {
                    printf1.setText("");
                    printf2.setText("");
                    printf3.setText("");
                    printf4.setText("");
                    flag1 =flag2=0;
                    My1.printf(printf1,".");
                }
                else {My3.printf(printf3,".");soundPool.play(sp[10],1,1,0,0,1);}
            }
        });
        Plus.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My2.printf(printf2,"+");
                soundPool.play(sp[11],1,1,0,0,1);
                flag1 =1;
            }
        });
        Less.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My2.printf(printf2,"-");
                soundPool.play(sp[12],1,1,0,0,1);
                flag1 =2;
            }
        });
        Multiply.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My2.printf(printf2,"×");
                soundPool.play(sp[13],1,1,0,0,1);
                flag1 =3;
            }
        });
        Divide.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My2.printf(printf2,"÷");
                soundPool.play(sp[14],1,1,0,0,1);
                flag1 =4;
            }
        });
        Delete.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {           //这个是清屏建
                printf1.setText("");
                printf2.setText("");
                printf3.setText("");
                printf4.setText("");
                soundPool.play(sp[16],1,1,0,0,1);
                flag1=flag2 =0;
            }
        });
        Equal.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My4.printf(printf4,"=");

                String StrNO1=My1.mytext();
                String StrNO2=My3.mytext();
                double NO1= Double.parseDouble(StrNO1);
                double NO2= Double.parseDouble(StrNO2);
                switch (flag1)
                {
                    case 1:result=NO1+NO2;break;
                    case 2:result=NO1-NO2;break;
                    case 3:result=NO1*NO2;break;
                    case 4:result=NO1/NO2;break;
                }
                if(result==100){flag2=1;}
                if (flag1==2&&result<=10&&result>0){flag2=2;}
                if (result==0){flag2=3;}
                if (NO1==1&&NO2==1&&flag1==1){flag2=4;}
                if (NO1==2&&NO2==2&&flag1==3){flag2=5;}
                if (NO1!=1&&NO2!=1&&flag1==1&&result<=10){flag2=6;}
                if (flag1==3&&NO1<10&&NO1>0&&NO2<10&&NO2>0&&NO1!=2&&NO2!=2){flag2=7;}
                switch (flag2)
                {
                    case 1:My4.printf(printf4,"我一百块都不给你!");soundPool.play(sp[17],1,1,0,0,1);break;
                    case 2:My4.printf(printf4,"你这个大笨蛋!");soundPool.play(sp[18],1,1,0,0,1);break;
                    case 3:My4.printf(printf4,"你这个大傻逼!");soundPool.play(sp[19],1,1,0,0,1);break;
                    case 4:My4.printf(printf4,"傻逼,大傻逼,傻到不会一加一!");soundPool.play(sp[20],1,1,0,0,1);break;
                    case 5:My4.printf(printf4,"我才不会告诉你二乘二等于你呢!");soundPool.play(sp[21],1,1,0,0,1);break;
                    case 6:My4.printf(printf4,"我这么高大上的计算器,怎么可能帮你算十以内的加法啦");soundPool.play(sp[22],1,1,0,0,1);break;
                    case 7:My4.printf(printf4,"九九乘法表,自己不会背哦!");soundPool.play(sp[23],1,1,0,0,1);break;
                    default:My4.printf(printf4,String.valueOf(result));soundPool.play(sp[15],1,1,0,0,1);break;
                }
                  flag1 =-1;
                  flag2=0;
            }
        });
    }}

最后是各种奇葩各种错的main_activity

错误一:id全找不到了

package chenyuwei.com.aftermath;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;




public class A extends Activity
{
    private ImageButton Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Left,Right,Equal,Point,Delete,Plus,Less,Multiply,Divide;
    private TextView printf1,printf2,printf3,printf4;
    private MyClick My=new MyClick();
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
        Zero=(ImageButton)findViewById(R.id.zero);
        One=(ImageButton)findViewById(R.id.one);
        Two=(ImageButton)findViewById(R.id.two);
        Three=(ImageButton)findViewById(R.id.three);
        Four=(ImageButton)findViewById(R.id.four);
        Five=(ImageButton)findViewById(R.id.five);
        Six=(ImageButton)findViewById(R.id.six);
        Seven=(ImageButton)findViewById(R.id.seven);
        Eight=(ImageButton)findViewById(R.id.eight);
        Nine=(ImageButton)findViewById(R.id.nine);
        Left=(ImageButton)findViewById(R.id.left);
        Right=(ImageButton)findViewById(R.id.right);
        Equal=(ImageButton)findViewById(R.id.equal);
        Point=(ImageButton)findViewById(R.id.point);
        Delete=(ImageButton)findViewById(R.id.jiong);
        Plus=(ImageButton)findViewById(R.id.plus);
        Less=(ImageButton)findViewById(R.id.less);
        Multiply=(ImageButton)findViewById(R.id.multiply);
        Divide=(ImageButton)findViewById(R.id.divide);
        printf1=(TextView)findViewById(R.id.printf1);
        printf2=(TextView)findViewById(R.id.printf2);
        printf3=(TextView)findViewById(R.id.printf3);
        printf4=(TextView)findViewById(R.id.printf4);

        Zero.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                My.judge(printf1,printf3, "0");
            }
        });
        One.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view)
            {
                My.judge(printf1,printf3,"1");
            }
        });
        Two.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                My.judge(printf1,printf3, "2");
            }
        });
        Three.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                My.judge(printf1,printf3,"3");
            }
        });
        Four.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My.judge(printf1,printf3, "4");
            }
        });
        Five.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My.judge(printf1,printf3, "5");
            }
        });
        Six.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My.judge(printf1,printf3, "6");
            }
        });
        Seven.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My.judge(printf1,printf3, "7");
            }
        });
        Eight.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My.judge(printf1,printf3, "8");
            }
        });
        Nine.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My.judge(printf1,printf3, "9");
            }
        });
        Point.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My.judge(printf1,printf3, ".");
            }
        });
        Plus.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My.printf(printf2,"+");
            }
        });
        Less.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My.printf(printf2,"-");
            }
        });
        Multiply.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My.printf(printf2,"×");
            }
        });
        Divide.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My.printf(printf2,"÷");
            }
        });

看起来好像是对的,但是在Android studio中是如下图的效果,而我把项目重建,id又都找到了,重建采取单纯的复制粘贴模式,所以应该不是代码的错,在studio和eclipse中都有过这样的情况,小白不知到底哪里错了
(放图)

错误二,我想用BigDecimal来得到更加精准的数据,但是运行时一进去就闪退,这一次,我只用了一个TextView(用来打印显示文字的组件),也就是说,输入后,例如:“1+1=”都会在一排,需要把“1”“+”“1”和“=”都识别出来,于是设计了一个分割的方法,如下

package chenyuwei.com.aftermath1;

import android.widget.TextView;

/**
 * Created by Administrator on 2015/2/8.
 */
import java.math.BigDecimal;


public class MyClick
{

    String text;
    String StringNO1="";
    String StringNO2="";
    BigDecimal NO1=new BigDecimal(Double.parseDouble(StringNO1));
    BigDecimal NO2=new BigDecimal(Double.parseDouble(StringNO2));
    public void printf(TextView c, String now)
    {
        String pervious;
        pervious=(String)c.getText();
        text=pervious+now;
        c.setText(text);
    }
    public double answer(String str)
    {
        for(int i=0;ifor(int i=text.indexOf(str)+1;i1;i++)
        {
            StringNO2=StringNO2+String.valueOf(text.charAt(i));
        }
        System.out.println(StringNO1);
        System.out.println(StringNO2);
        double result=NO1.add(NO2).doubleValue();
        return result;
    }

}

错误三,再次尝试用BigDecimal,反正不知怎么地就是闪退

main_activity:

package chenyuwei.com.aftermath;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.*;
import android.view.View;

public class MainActivity extends Activity {
    private ImageButton Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Left,Right,Equal,Point,Delete,Plus,Less,Multiply,Divide;
    private TextView printf1,printf2,printf3,printf4;
    private MyClick My1=new MyClick();
    private MyClick My2=new MyClick();
    private MyClick My3=new MyClick();
    private MyClick My4=new MyClick();
    private int flag=0;
    double Result;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Zero=(ImageButton)findViewById(R.id.zero);
        One=(ImageButton)findViewById(R.id.one);
        Two=(ImageButton)findViewById(R.id.two);
        Three=(ImageButton)findViewById(R.id.three);
        Four=(ImageButton)findViewById(R.id.four);
        Five=(ImageButton)findViewById(R.id.five);
        Six=(ImageButton)findViewById(R.id.six);
        Seven=(ImageButton)findViewById(R.id.seven);
        Eight=(ImageButton)findViewById(R.id.eight);
        Nine=(ImageButton)findViewById(R.id.nine);
        Left=(ImageButton)findViewById(R.id.left);
        Right=(ImageButton)findViewById(R.id.right);
        Equal=(ImageButton)findViewById(R.id.equal);
        Point=(ImageButton)findViewById(R.id.point);
        Delete=(ImageButton)findViewById(R.id.jiong);
        Plus=(ImageButton)findViewById(R.id.plus);
        Less=(ImageButton)findViewById(R.id.less);
        Multiply=(ImageButton)findViewById(R.id.multiply);
        Divide=(ImageButton)findViewById(R.id.divide);
        printf1=(TextView)findViewById(R.id.printf1);
        printf2=(TextView)findViewById(R.id.printf2);
        printf3=(TextView)findViewById(R.id.printf3);
        printf4=(TextView)findViewById(R.id.printf4);

        Zero.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (flag==0)My1.printf(printf1, "0");
                else My3.printf(printf3,"0");
            }
        });
        One.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view)
            {
                if (flag==0)My1.printf(printf1, "1");
                else My3.printf(printf3,"1");
            }
        });
        Two.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                if (flag==0)My1.printf(printf1, "2");
                else My3.printf(printf3,"2");
            }
        });
        Three.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                if (flag==0)My1.printf(printf1, "3");
                else My3.printf(printf3,"3");
            }
        });
        Four.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (flag==0)My1.printf(printf1, "4");
                else My3.printf(printf3,"4");
            }
        });
        Five.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (flag==0)My1.printf(printf1, "5");
                else My3.printf(printf3,"5");
            }
        });
        Six.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (flag==0)My1.printf(printf1, "6");
                else My3.printf(printf3,"6");
            }
        });
        Seven.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (flag==0)My1.printf(printf1, "7");
                else My3.printf(printf3,"7");
            }
        });
        Eight.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (flag==0)My1.printf(printf1, "8");
                else My3.printf(printf3,"8");
            }
        });
        Nine.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (flag==0)My1.printf(printf1, "9");
                else My3.printf(printf3,"9");
            }
        });
        Point.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (flag==0)My1.printf(printf1, ".");
                else My3.printf(printf3,".");
            }
        });
        Plus.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My2.printf(printf2, "+");
                flag=1;
            }
        });
        Less.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My2.printf(printf2, "-");
                flag=2;
            }
        });
        Multiply.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My2.printf(printf2, "×");
                flag=3;
            }
        });
        Divide.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My2.printf(printf2, "÷");
                flag=4;
            }
        });
        Equal.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                My4.printf(printf4, "=");

                }

        });

    }

MyClick.java:

package chenyuwei.com.aftermath;

/**
 * Created by Administrator on 2015/2/9.
 */

import android.widget.TextView;
import java.math.BigDecimal;
public class MyClick
{
    String text;
    String StringNO1="";
    String StringNO2="";
    BigDecimal NO1=new BigDecimal(Double.parseDouble(StringNO1));
    BigDecimal NO2=new BigDecimal(Double.parseDouble(StringNO2));
    public void printf(TextView c, String now)
    {
        String pervious;
        pervious=(String)c.getText();
        text=pervious+now;
        c.setText(text);
    }
    /*public String answer(String str)
    {
        for(int i=0;i
    public String get()
    {
        return text;
    }
}

这个是后来才自己明白的,原来soundpool的很多方法只能放在函数中
()
最后,看看我的会发脾气的计算器效果吧,哈哈。

http://xiangce.baidu.com/picture/album/list/cf18974536e15700b2a2d56482daffb3d9e6ad91?isscore=1#isScore

你可能感兴趣的:(Android开发,android,软件,日志)