1

我完全是这个 android 的初学者,我已经在这个障碍上待了两天,任何帮助都是可观的

当我在屏幕上按下某个数字时,它会出现,然后我按下某个符号按钮,它接受并清除屏幕,然后我再次输入新数字,但当我按下相等按钮时它会出现“崩溃”。我认为问题是当调用相等的单击函数并且在其中 .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: {
        }

    }


}
4

3 回答 3

4

请注意,当您android:onClick输入 aButton时,例如

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

定义一个名称为参数值的方法android:onClickView否则可能会NoSuchMethod出错

public void equalclicked(View v)
{
}

这仅适用于Activity不在任何片段中

于 2013-06-25T00:40:45.003 回答
1

尝试以下

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

于 2013-06-25T00:33:47.363 回答
1

您需要在 OnCreate 中编写一些内容,即调用 setContentView 并将其传递给您的布局文件。它崩溃了,因为您引用了一个不存在的按钮(如果 findViewById 没有找到它返回 null 的视图)。

在你的 onCreate 函数中写

setContentView(R.layout.yourlayoutfilename)
于 2013-06-25T00:34:57.160 回答