简体   繁体   English

我在简单的 android 应用程序中做错了什么?

[英]What am i doing wrong at my simple android app?

I am trying to make a app that has a switch a button and a text and if you turn the switch on and press the button;我正在尝试制作一个具有开关按钮和文本的应用程序,如果您打开开关并按下按钮; the number displayed on the text will be added by 1. But if the switch is turned off the number will be subtracted by 1. but when i run my app and press the button, the app crashes...文本上显示的数字将加 1。但如果关闭开关,数字将减 1。但是当我运行我的应用程序并按下按钮时,应用程序崩溃了...

i do not have much experience at programming and i do not know what im doing wrong.我在编程方面没有太多经验,我不知道我做错了什么。 and i have only tried this code.我只试过这个代码。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final TextView text = (TextView)findViewById(R.id.textView);
    final Button button = (Button)findViewById(R.id.button);
    Switch mySwitch = (Switch)findViewById(R.id.mySwitch);

    mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked== true){

                    button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            String text_string = text.getText().toString();
                            int text_int = Integer.parseInt(text_string);
                            text_int++;
                            text.setText(text_int);

                        }
                    });


                }

                if (isChecked == false) {

                    button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            String text_string = text.getText().toString();
                            int text_int = Integer.parseInt(text_string);
                            text_int++;
                            text.setText(text_int);

                        }
                    });

                }
        }
    });
}

} }

so this should behave as i described earlier but it doesn't.所以这应该像我之前描述的那样表现,但事实并非如此。

You don't need a listener for the Switch , but only 1 listener for the Button :您不需要Switch的侦听Switch ,但Button只需要 1 个侦听器:

final TextView text = (TextView)findViewById(R.id.textView);
final Button button = (Button)findViewById(R.id.button);
final Switch mySwitch = (Switch)findViewById(R.id.mySwitch);

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String text_string = text.getText().toString();
        int text_int = 0;
        try {
            text_int = Integer.parseInt(text_string);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        if (mySwitch.isChecked())
            text_int++;
        else
            text_int--;
        text.setText("" + text_int);
    }
});

Every time you click the Button , in its listener the value in the TextView is increased or decreased depending on whether the Switch is checked or not.每次单击Button ,在其侦听器中, TextView的值会根据是否选中 Switch 增加或减少。

You're nesting listeners but that logic doesn't work sequentially.您正在嵌套侦听器,但该逻辑无法按顺序工作。 You should declare your listeners separately.您应该单独声明您的听众。 I suggest you create a boolean that holds the state of the switch and one button listener.我建议你创建一个布尔值来保存开关的状态和一个按钮的监听器。 Within the listener check if switch is enabled then run your calculations and do the same if the switch is disabled.在侦听器中检查开关是否已启用,然后运行您的计算,如果开关已禁用,则执行相同的操作。

    button.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
       if(mySwitch.isChecked(){
           String text_string = text.getText().toString();
           int text_int = Integer.parseInt(text_string);
           text_int++;
           text.setText(String.valueOf(text_int));
        } else {
           String text_string = text.getText().toString();
           int text_int = Integer.parseInt(text_string);
           text_int++;
           text.setText(String.valueOf(text_int));
        }
      }
   });

Your app crashes because you are trying to set an int to a textview.setText() and when you pass an int to this method it expects it to be a resource id and which could not be found in your case that's why it will throw ResourceNotFoundException and crashes.您的应用程序崩溃是因为您试图将一个 int 设置为textview.setText()并且当您将一个 int 传递给此方法时,它期望它是一个资源 ID,并且在您的情况下无法找到,这就是它会抛出ResourceNotFoundException的原因和崩溃。

You should set text as following:您应该将文本设置如下:

text.setText(String.valueOf(text_int));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM