简体   繁体   English

在活动中没有从 EditText 获得任何价值

[英]Not getting any value from EditText in activity

I know that this might look stupid but still I'm getting frustrated as to why this edittext returns empty value.我知道这可能看起来很愚蠢,但我仍然对为什么这个 edittext 返回空值感到沮丧。

ImageButton attach = findViewById(R.id.btn_attach);
Button send = findViewById(R.id.btn_send);
EditText message = findViewById(R.id.report_message);

content = message.getText().toString(); // <!-- always null

send.setOnClickListener(v -> {
     if (searchSpinner.getSelectedItems().size() == 0 || content.equals("")) {
         Toast.makeText(getApplicationContext(), content + " > " + categoryList.toString().substring(1, categoryList.toString().length()-1),Toast.LENGTH_LONG).show();
     } else {
            if (imageList.size() != 0) {
                doMultipartRequest();
            } else {
                doSendRequest();
            }
    }
});

Can't think of any possible cause except that I accidentally clicked on something and it created a duplicate layout and generated a directory in layouts folder with x24 something on its name想不出任何可能的原因,除了我不小心点击了某个东西,它创建了一个重复的布局,并在布局文件夹中生成了一个目录,名称为 x24

You need to get text from your EditText inside setOnClickListener()您需要从setOnClickListener()内的EditText获取文本

Try this尝试这个

 send.setOnClickListener(v -> {
        content = message.getText().toString(); // <!-- always null
        if (searchSpinner.getSelectedItems().size() == 0 || content.equals("")) {
            Toast.makeText(getApplicationContext(),
                    content + " > " +
                            categoryList.toString().substring(1, categoryList.toString().length()-1),
                    Toast.LENGTH_LONG).show();
        } else {
            if (imageList.size() != 0) {
                doMultipartRequest();
            } else {
                doSendRequest();
            }
        }
    });

Try this:-尝试这个:-

  ImageButton attach = findViewById(R.id.btn_attach);
  Button send = findViewById(R.id.btn_send);
  EditText message = findViewById(R.id.report_message);


  send.setOnClickListener(v -> {
       if (searchSpinner.getSelectedItems().size() == 0 || message.getText().toString().equals("")) {
            Toast.makeText(getApplicationContext(),
               message.getText().toString()+ " > " +
                       categoryList.toString().substring(1, categoryList.toString().length()-1),
                        Toast.LENGTH_LONG).show();
       } else {
            if (imageList.size() != 0) {
                 doMultipartRequest();
            } else {
                 doSendRequest();
                }
            }
      });

Hope it will work希望它会起作用

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

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