android - 在不使用保存按钮的情况下保存editText,应用停止运行

标签 android crash rfid

我希望能够存储editText输入的内容,然后能够在列表 View 中显示它。
我已经连接了设置为仿真器模式的RFID设备。基本上,此模式也可以扫描RFID标签,并且无论鼠标光标位于何处,RFID编号都会被填充。在这种情况下,它位于editText输入处。由于RFID编号为10位,因此RFID编号的长度为10。一旦检测到RFID编号,我便希望将其显示在列表 View 中,并扫描另一个标签,并将其也添加到列表 View 中。
就我而言,只要我的代码看到显示方法,应用程序就会崩溃,我也不知道为什么。有人可以向我解释为什么会这样吗?


epc.add("\n" + etRfidNo.getText().toString() + ", " + DateFormat.getInstance().format(currentDate));

display();```

MainActivity code:

    public class MainActivity extends AppCompatActivity {

     EditText etRfidNo;
     TextView textView;
     private Set<String> epc = new HashSet<>();
     ArrayAdapter<String> contactAdapter;
     String single_epc;
     Button scan;
     ListView listView;
     boolean set = true;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView)findViewById(R.id.textView);
        etRfidNo = (EditText) findViewById(R.id.etRfidNo);
        scan = (Button) findViewById(R.id.scan);
        TextView textV = (TextView)findViewById(R.id.textView);



        etRfidNo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //TextView textV = (TextView)findViewById(R.id.textView);
                //textV.setText(s); //set text for text view

                single_epc = String.valueOf(s);
                if(s.length() == 10)
                {
                    Date currentDate = new Date();
                    epc.add("\n" + etRfidNo.getText().toString() + ", " + DateFormat.getInstance().format(currentDate));
                    display();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });



     }

     public void display() {
        contactAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<>(epc));

        listView.setAdapter(contactAdapter);

     }

    }

最佳答案

listView = (ListView) findViewById(R.id.listviewID);
忘记添加此。

关于android - 在不使用保存按钮的情况下保存editText,应用停止运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63309846/

相关文章:

javascript - RFID阅读器与网页集成

android - 您如何在 firestore android 的事务中遍历子集合中的文档?

android - 限制 Bottom Sheet 单向下滑动

C++ 无法理解的崩溃行为

debugging - 怎么会发生? (在调试 minidump 文件中)

android - 什么是 D/dalvikvm(831) : Not late-enabling CheckJNI (already on)

android - 用 Android 写 "NfcA only"标签

android - ISO15693/Tag-it HF-I 收发失败

java - AutoCompleteTextView 不响应对其 ArrayAdapter 的更改

android - 屏幕旋转后 Activity 和 Intent 服务通信中断