当我在我的Android应用程序(计算器)上按相同的按钮.....它崩溃

时间:2013-06-25 00:29:26

标签: android string

我完全是这个机器人的初学者,我已经在这个障碍两天了,任何帮助都很明显

当我按下屏幕上的某个数字时,它会出现,然后我按下它接受的一个符号按钮并清除屏幕,然后我再次输入新的数字,但是当我按下相同的按钮时,它就显示为C R A S H E S'。我认为问题是当调用相等的单击函数并在其中.settext(“”+ ans)正在解决问题我尝试了其他替代方法,如String.valueof(ans)然后将其发送出去..仍然崩溃

注意:我在onCreate函数中没有写任何内容

XML代码:

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/dark"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:textAlignment="gravity"
tools:context=".MainActivity" >

<Button
    android:id="@+id/button7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button6"
    android:layout_alignBottom="@+id/button6"
    android:layout_toRightOf="@+id/button6"
    android:onClick="button_pressed"
    android:text="@string/SEVEN"
    android:textColor="#24578a" />

<Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button8"
    android:layout_alignBottom="@+id/button8"
    android:layout_alignLeft="@+id/button6"
    android:onClick="button_pressed"
    android:text="@string/FIVE"
    android:textColor="#24578a" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button5"
    android:layout_toRightOf="@+id/button3"
    android:onClick="button_pressed"
    android:text="@string/FOUR"
    android:textColor="#24578a" />   


<Button
    android:id="@+id/button9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button8"
    android:layout_toRightOf="@+id/button5"
    android:onClick="button_pressed"
    android:text="@string/NINE"
    android:textColor="#24578a" />

<Button
    android:id="@+id/button8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button7"
    android:layout_alignLeft="@+id/button7"
    android:onClick="button_pressed"
    android:text="@string/EIGHT"
    android:textColor="#24578a" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button4"
    android:layout_alignBottom="@+id/button4"
    android:layout_toLeftOf="@+id/button5"
    android:onClick="button_pressed"
    android:text="@string/THREE"
    android:textColor="#24578a" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button6"
    android:layout_toLeftOf="@+id/button5"
    android:onClick="button_pressed"
    android:text="@string/TWO"
    android:textColor="#24578a" />

<Button
    android:id="@+id/button6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button12"
    android:layout_toRightOf="@+id/button1"
    android:onClick="button_pressed"
    android:text="@string/SIX"
    android:textColor="#24578a" />

<Button
    android:id="@+id/button12"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button10"
    android:layout_alignBottom="@+id/button10"
    android:layout_toRightOf="@+id/button6"
    android:text="@string/EQUAL"
    android:onClick="equalclicked"
     />

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="5sp"
    android:layout_marginRight="5sp"
    android:layout_marginTop="22dp"
    android:gravity="right"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#ffff"
    android:textIsSelectable="true" />

<Button
    android:id="@+id/buttonmn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button4"
    android:layout_alignRight="@+id/button9"
    android:text="@string/MINUS"
    android:textColor="#24578a"
    android:onClick="signclicked"
     />

<Button
    android:id="@+id/buttonp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button10"
    android:layout_alignBottom="@+id/button10"
    android:layout_alignLeft="@+id/textView1"
    android:layout_marginLeft="20dp"
    android:onClick="signclicked"
    android:text="@string/PLUS"
    android:textColor="#24578a" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/buttonp"
    android:layout_alignLeft="@+id/buttonp"
    android:onClick="button_pressed"
    android:text="@string/ONE"
    android:textColor="#24578a" />

<Button
    android:id="@+id/button10"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="42dp"
    android:layout_toRightOf="@+id/buttonp"
    android:onClick="button_pressed"
    android:text="@string/ZERO"
    android:textColor="#24578a" />

<Button
    android:id="@+id/buttond"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/buttonmn"
    android:layout_alignRight="@+id/button4"
    android:text="@string/DIVIDE"
    android:textColor="#24578a"
    android:onClick="signclicked"
     />

<Button
    android:id="@+id/buttonmm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button4"
    android:layout_toLeftOf="@+id/button4"
    android:onClick="signclicked"
    android:text="@string/MULTIPLY"
    android:textColor="#24578a" />

主要活动

boolean screenwithvalue = false;
int tag = 0;
int tag2 = 0;
int value1;
int value2;
char sign;
int ans;

public String currentscreenstate() {

    TextView screencurrentstate = (TextView) findViewById(R.id.textView1);
    String text = screencurrentstate.getText().toString();
    return text;
}

public void button_pressed(View v) {

    TextView screencurrentstate = (TextView) findViewById(R.id.textView1);
    if (tag > 0) {
        screencurrentstate.setText("");
    } else {
    }

    //  String label   =    currentscreenstate();
    Button btnpressed = (Button) findViewById(v.getId());
    String updatescreen = btnpressed.getText().toString();
    screencurrentstate.append(updatescreen);
    screenwithvalue = true;
}

public void signclicked(View v) {

    TextView screen = (TextView) findViewById(R.id.textView1);
    Button signpressed = (Button) findViewById(v.getId());
    String buttonsign = signpressed.getText().toString();
    sign = buttonsign.charAt(0);

    if (v.getId() == R.id.buttonp | v.getId() == R.id.buttonmn | v.getId() == R.id.buttonmm | v.getId() == R.id.buttond) {

        if (screenwithvalue) {
            String valuesave1 = currentscreenstate();
            int x = Integer.parseInt(valuesave1);
            if (tag2 == 0) {
                value1 = x;
                tag2++;
                screen.setText("");

            } else {
            }

        } else {
        }
    } else {
    }

}

public void equalclicked() {

    TextView screen2 = (TextView) findViewById(R.id.textView1);
    String valuesave2 = currentscreenstate();
    int x = Integer.parseInt(valuesave2);

    if (tag2 == 1) {
        value2 = x;
        screen2.setText("");
    }

    switch (sign) {

        case '+': {
            ans = value1 + value2;

            screen2.setText("" + ans);

            tag2 = 0;
            break;
        }

        case '-': {
            ans = value1 - value2;

            screen2.setText("" + ans);

            tag2 = 0;
            break;
        }

        case '*': {
            ans = value1 * value2;
            screen2.setText("" + ans);

            tag2 = 0;
            break;
        }

        case '/': {
            ans = value1 / value2;
            screen2.setText("" + ans);

            tag2 = 0;
            break;
        }

        default: {
        }

    }


}

3 个答案:

答案 0 :(得分:4)

请注意android:onClick放在Button中,例如

<Button ... android:onClick="equalclicked" />

使用android:onClick参数定义一个名称为View的值的方法,否则可能会出现NoSuchMethod错误

public void equalclicked(View v)
{
}

这仅适用于Activity而不是任何片段

答案 1 :(得分:1)

尝试以下

public void equalclicked( 查看v ){  你的代码...... }

答案 2 :(得分:1)

你需要在OnCreate中写一些东西,即调用setContentView,你将布局文件传递给它。它崩溃是因为你引用了一个不存在的按钮(如果findViewById找不到它返回null的视图)。

在你的onCreate函数中写

setContentView(R.layout.yourlayoutfilename)