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

时间:2019-02-11 15:48:28

标签: java android android-studio

我正在尝试制作一个带有开关的应用程序,一个按钮和一个文本,如果您打开开关并按一下按钮,文本上显示的数字将加1。但是,如果关闭开关,数字将减1。 但是当我运行我的应用程序并按下按钮时,该应用程序崩溃了...

我没有太多的编程经验,我也不知道我做错了什么。而且我只尝试了这段代码。

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);

                        }
                    });

                }
        }
    });
}

}

所以这应该像我之前描述的那样,但事实并非如此。

3 个答案:

答案 0 :(得分:0)

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);
    }
});

每次单击Button时,在其监听器中,TextView中的值根据是否选中该开关而增加或减小。

答案 1 :(得分:0)

您正在嵌套侦听器,但该逻辑无法按顺序进行。您应该分别声明您的听众。我建议您创建一个布尔值,以保存开关和一个按钮侦听器的状态。在侦听器中,检查是否启用了开关,然后运行计算,如果禁用了开关,则执行相同的操作。

    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));
        }
      }
   });

答案 2 :(得分:0)

您的应用程序崩溃是因为您尝试将int设置为textview.setText(),并且当您将int传递给此方法时,它期望它是资源ID,因此在您的情况下找不到它,这就是为什么将抛出ResourceNotFoundException并崩溃。

您应将文本设置如下:

text.setText(String.valueOf(text_int));
相关问题