Application terminates unexpectedly [duplicate]

左心房为你撑大大i 提交于 2021-01-29 06:19:42

问题


public class MainActivity extends AppCompatActivity {

public String[] epc;
public int i = 0;

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



    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

public void multipleRead(View view)
{
    try {
        RFIDReader reader = RFID.open();

        reader.inventory(new RFIDCallback() {
                             @Override
                             public void onTagRead(Tag tag) {
                                 epc[i] = tag.getEPC();
                                 i++;
                             }
                         }
        );

    }
    catch (ReaderException e) {
        Toast.makeText(this, "Error: " + e, Toast.LENGTH_LONG).show();
    }
}
}

I am making a simple application that reads the ID from an RFID tag ad then displays it on the screen. The reader.inventory is called to scan multiple tags at once. I then store these tags ID number in a string array epc. The method multipleRead() is attached to a scan button on the application. So everytime the button is pressed, the multipleRead() is called.

However after I load the apk on the device and click on the button I get the following error:

E/AndroidRuntime: FATAL EXCEPTION: Thread-325
                  Process: com.example.user.myapplication, PID: 3484
                  java.lang.NullPointerException
                      at com.example.user.myapplication.MainActivity$1.onTagRead(MainActivity.java:79)
                      at com.alien.rfid.a$1.run(Unknown Source)
                      at java.lang.Thread.run(Thread.java:841)

I have read other answers similar to this but it as not helped in solving this issue. How do I get rid on the runtime error?

Updated code and error:

public class MainActivity extends AppCompatActivity {

  private List<String> epc = new ArrayList<>();
  private TextView textView;
  @Override

  public void multipleRead(View view)
  {
    try {
    // Get global RFID Reader instance
        RFIDReader reader = RFID.open();

        reader.inventory(new RFIDCallback() {
                             @Override
                             public void onTagRead(Tag tag) {
                                 epc.add(tag.getEPC());
                                 textView.append(tag.getEPC() + "\n");
                             }
                         }
        );

    }
    catch (ReaderException e) {
        Toast.makeText(this, "Error: " + e, Toast.LENGTH_LONG).show();
    }
  }

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

    textView = (TextView)findViewById(R.id.tag_id);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
  }
}

Error

E/AndroidRuntime: FATAL EXCEPTION: Thread-347
Process: com.example.user.myapplication, PID: 25039
         android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

         at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6669)
         at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1005)
         at android.view.ViewGroup.invalidateChild(ViewGroup.java:4548)
         at android.view.View.invalidate(View.java:11095)
         at android.view.View.invalidate(View.java:11044)
         at android.widget.TextView.checkForRelayout(TextView.java:6760)
         at android.widget.TextView.setText(TextView.java:3850)
         at android.widget.TextView.setText(TextView.java:3708)
         at android.widget.TextView.append(TextView.java:3443)
         at android.widget.TextView.append(TextView.java:3433)
         at com.example.user.myapplication.MainActivity$1.onTagRead(MainActivity.java:80)
         at com.alien.rfid.a$1.run(Unknown Source)
         at java.lang.Thread.run(Thread.java:841)

来源:https://stackoverflow.com/questions/62987177/application-terminates-unexpectedly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!