在editBox Android Studio和按钮监听器中显示结果

时间:2016-06-01 16:40:10

标签: java android

除了我假设的逻辑之外,目前没有任何错误。模拟器启动但是当单击按钮时,家庭作业应用程序崩溃我是否需要在每个按钮上添加onClickListeners,或者还有其他问题。谢谢你的帮助

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Button one = (Button) findViewById(R.id.button1);
        one.setOnClickListener(this); // calling onClick() method
        Button two = (Button) findViewById(R.id.button2);
        two.setOnClickListener(this);
        Button three = (Button) findViewById(R.id.button3);
        three.setOnClickListener(this);
        Button four = (Button) findViewById(R.id.button4);
        four.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {

        EditText myTxt = (EditText) findViewById(R.id.editText4);

        String n1 = myTxt.getText().toString();

        int x = Integer.parseInt(n1);

        EditText myTxt2 = (EditText) findViewById(R.id.editText5);

        String n2 = myTxt2.getText().toString();

        int y = Integer.parseInt(n2);

        EditText result = (EditText)findViewById(R.id.editText6);

        int z = 0;



        switch(v.getId())
        {
            case R.id.button1:
                z = x + y;
                break;

            case R.id.button2:
                z = x - y;
                break;

            case R.id.button3:
                z = x * y;
                break;

            case R.id.button4:
                if (y != 0) {

                    z = x / y;

                    break;
                }


                else {

                    EditText text = (EditText)findViewById(R.id.editText3);
                    text.setText("Cannot Divide By Zero");
                    break;

                }

            default:
                break;

        }

        result.setText(z);
    }

    }

06-01 13:10:58.285 10844-10844 / com.example.andrey.homework E / AndroidRuntime:FATAL EXCEPTION:main 06-01 13:10:58.285 10844-10844 / com.example.andrey.homework E / AndroidRuntime:流程:com.example.andrey.homework,PID:10844 06-01 13:10:58.285 10844-10844 / com.example.andrey.homework E / AndroidRuntime:android.content.res.Resources $ NotFoundException:String resource ID#0x1 06-01 13:10:58.285 10844-10844 / com.example.andrey.homework E / AndroidRuntime:在android.content.res.Resources.getText(Resources.java:312) 06-01 13:10:58.285 10844-10844 / com.example.andrey.homework E / AndroidRuntime:at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52) 06-01 13:10:58.285 10844-10844 / com.example.andrey.homework E / AndroidRuntime:at android.widget.TextView.setText(TextView.java:4417) 06-01 13:10:58.285 10844-10844 / com.example.andrey.homework E / AndroidRuntime:at com.example.andrey.homework.MainActivity.onClick(MainActivity.java:112) 06-01 13:10:58.285 10844-10844 / com.example.andrey.homework E / AndroidRuntime:at android.view.View.performClick(View.java:5198) 06-01 13:10:58.285 10844-10844 / com.example.andrey.homework E / AndroidRuntime:at android.view.View $ PerformClick.run(View.java:21147) 06-01 13:10:58.285 10844-10844 / com.example.andrey.homework E / AndroidRuntime:at android.os.Handler.handleCallback(Handler.java:739) 06-01 13:10:58.285 10844-10844 / com.example.andrey.homework E / AndroidRuntime:at android.os.Handler.dispatchMessage(Handler.java:95) 06-01 13:10:58.285 10844-10844 / com.example.andrey.homework E / AndroidRuntime:at android.os.Looper.loop(Looper.java:148) 06-01 13:10:58.285 10844-10844 / com.example.andrey.homework E / AndroidRuntime:at android.app.ActivityThread.main(ActivityThread.java:5417) 06-01 13:10:58.285 10844-10844 / com.example.andrey.homework E / AndroidRuntime:at java.lang.reflect.Method.invoke(Native Method) 06-01 13:10:58.285 10844-10844 / com.example.andrey.homework E / AndroidRuntime:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 06-01 13:10:58.285 10844-10844 / com.example.andrey.homework E / AndroidRuntime:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

2 个答案:

答案 0 :(得分:0)

import React, { Component } from 'react';

import {
  StyleSheet,
  Text,
  TextInput,
  View
} from 'react-native';

class MessageEntry extends Component {  
  constructor(props) {
    super(props);

    this.state = {
      content: ''
    }
  }

  render() {
    return (
      <View>
        <TextInput ref="newMessageContent"
          style={styles.input}
          multiline={true}
          placeholder="Ask me anything! :-)"
          onChangeText={(content) => this.setState({content: content})}
          value={this.state.content}
        />

        <Text onPress={() => this.sendMessage(this.props.chatRef)} style={styles.button}>Send</Text>
      </View>
    )
  }

  sendMessage(chatRef) {
    if (this.state.content == '') { return false; }
    chatRef.push({
      content: this.state.content,
      sent_at: Firebase.ServerValue.TIMESTAMP,
      self: true
    });

    this.setState({content: ''});
  }

}

export { MessageEntry }

检查这2行...可能是你引用相同的ID。

请分享日志。

答案 1 :(得分:0)

根据堆栈跟踪,问题似乎是此调用result.setText(z)

您使用的setText(int resid)方法将EditText视图的文本设置为String资源,并将您作为参数传递给此函数的ID,问题是没有任何具有该ID的资源。

您应该使用的是result.setText(String.valueOf(z)),而是使用setText(String str)方法。这会将EditText视图的文本设置为z变量的值。

回复您的评论:“如何在更改数字值后删除错误消息?”

您可以这样做:

EditText myTxt = ...;
EditText myTxt2 = ...;
EditText result = ...;

TextWatcher w = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void afterTextChanged(Editable editable) {
        if("Cannot Divide By Zero".equals(result.getText().toString())){// the "result" contains the error message
            if(editable.matches("^\\-?\\d+$")){// the `EditText` contain a number (can be a negative number).
                result.setText("");// clear the error message.
            }
        }
    }
};

myTxt.addTextChangedListener(w);
myTxt2.addTextChangedListener(w);