I am having an issue where I can not sort out the NullPointerException
(NPE) that I am getting.
Here is my MainActivity code
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
String bccode = result.getContents();
if(result != null) {
if(result.getContents() == null) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Intent sendBarCode= new Intent(MainActivity.this, showdata.class);
sendBarCode.putExtra("barccode", bccode);
startActivity(sendBarCode);
startActivity(new Intent(MainActivity.this, showdata.class));
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
And my class that receives it showdata.class:
public String barcode = getIntent().getStringExtra("barccode");
The exact error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
This particular NPE has me a bit perplexed as I have followed all the instructions I can find online.
Protect from NPE
final Intent intent = getIntent();
if (intent != null) {
...
}
else {
Log.d(TAG, "No intent");
}
因此,您要两次调用startActivity
,一次是使用数据,一次是使用不会包含任何数据的新Intent
。
TRY This out in the showdata
class.
public String barcode = null;
Intent i = getIntent();
barcode =i.getStringExtra("barccode");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.