I am having some issues with ZXing's barcode scanner in my android application. I asked a question here yesterday that explains a null pointer error that I was having. The issue was solved and just showed that the cursor wasnt retrieving any information back.
My issues is that I know query is correct and I know the code is correct because I have tested it out without the scanner separately and it all works fine. Is it possible that I am calling to the database wrong in the barcode scanner intent or is there a certain way that I should be doing it?
Any help or guidance would be great!
onActivity Result
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
//get the extras that are returned from the intent
barcode_number = intent.getStringExtra("SCAN_RESULT");
String bNumber = barcode_number.toString();
//Code to add scanned item to DB, needs barcode data before can be ruin
final Cursor barInformation = adapter.barcodeInfo(barcode_number);
barInformation.moveToFirst();
String itemName = barInformation.getString(barInformation.getColumnIndex("barcode_item_name"));
Toast toast = Toast.makeText(this, "Barcode Number:" + itemName ,Toast.LENGTH_SHORT);
toast.show();
}
}
}
Query
public Cursor barcodeInfo(String number){
// Safe check to make sure db and number is not null
if(db == null || number == null){
return null;
}
return db.rawQuery("select _id, barcode_item_name, barcode_measurement, barcode_unit from barcode where barcode_number = ?", new String[]{number});
}
Error
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference
02-12 11:15:07.544 12808-12808/com.example.rory.prototypev2 E/AndroidRuntime: at com.example.rory.prototypev2.DBMain.barcodeInfo(DBMain.java:437)
02-12 11:15:07.544 12808-12808/com.example.rory.prototypev2 E/AndroidRuntime: at com.example.rory.prototypev2.scanner.onActivityResult(scanner.java:77)
Try like this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE: {
if (resultCode != RESULT_CANCELED) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
String strData = scanResult.getContents(); // use this
} else {
Toast.makeText(this, "Error message", Toast.LENGTH_LONG).show();
}
break;
}
}
}
I have fixed the issue. My problem was that I wasnt opening the database before I was calling the functions and thus the function was a null object. The correct code is as below.
adapter.open(); // I was missing this line
Cursor c = adapter.barcodeInfo(bNumber);
c.moveToPosition(-1);
while(c.moveToNext())
{
name = c.getString(c.getColumnIndex("barcode_item_name"));
measurement = c.getInt(c.getColumnIndex("barcode_measurement"));
unit = c.getString(c.getColumnIndex("barcode_unit"));
Toast toast = Toast.makeText(this, "Adding" + name + "to Kitchen", Toast.LENGTH_SHORT);
toast.show();
}
Related
am scan Qr code/Barcode, so I want to scan different qr code/barcode and arrange them is String listview.
Example I want to setText() like this ,scanNumber1,scanNumber2,....,..., so far i have managed to scan that Qr Code/Barcode but when i scan the next number it remove the first one, so I want ti arrange those number.
here is my code
public void scanCode(View view){
IntentIntegrator intentIntegrator = new IntentIntegrator(this);
intentIntegrator.initiateScan();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
String scanNumber=intentResult.getContents();
if (intentResult != null) {
if(intentResult.getContents() == null) {
Toast toast = Toast.makeText(getApplicationContext(), "Scan The QR code/Barcode", Toast.LENGTH_LONG);
toast.show();
}
//validate ur scan numbers
else if (intentResult.getContents().length() == 10) {
textView1.setText(intentResult.getContents());
}
else if(intentResult.getContents().length() == 12){
textView2.setText(intentResult.getContents());
}
else {
Toast toast = Toast.makeText(getApplicationContext(), "That Number is Out Of Our Range", Toast.LENGTH_LONG);
toast.show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
and this is output
so I want on that Slave to appear like ,scanNumber1,scanNumber2,....,...,
you can use a global JointoString function object to achieve the desired behaviour i.e. "barcode1","barcode2"....
ArrayList<String> results = new ArrayList<String>()
when you receive the result try
results.add(<value>)
while displaying to text view use
textView.setText(results.joinToString(","))
First of all sorry for my Bad English
i'm new To Android development.
I'm trying to develop this App in which user can click on a button and the Contacts will show up and then can pick one and only one one of the Contacts and show its number in a textbox.
But i face this Error every time :
"Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor
is initialized correctly before accessing data from it"
and The Funny thing is when i use HAS_PHONE_NUMBER it returns 1 which means the Contact has Phone Number.
and when i use DISPLAY_NAME it returns contact name without any problem.
but when i use NUMBER that Error shows Up!
can anyone help me?
BTW i use Fragment if it helps..?
and this is my code :
#Override
public void onClick(View v) {
if (v.getId() == R.id.btnContact) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT); }
}
private Uri uriContact;
private String contactID; // contacts unique ID
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor phone = getActivity().getContentResolver().query(contactData, null, null, null, null);
phone.moveToFirst();
if (phone.moveToFirst()) {
String num = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
// Todo something when contact number selected
placeholderNumber.setText("" + num);
}
}
break;
}
}
I am making an android studio app and I want it to make a TextView disappear when I say a certain phrase. The code saves everything I say into an array list. I want to see if it includes the phrase.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 100:
if (resultCode == RESULT_OK && data != null){
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
boolean yes = Arrays.asList(result.get(0)).contains(InputActivity.Item1);
if(yes == true){
result1.setVisibility(View.GONE);
}
}
break;
}
}
Use
for(String s:result){
if(s.equals(InputActivity.Item1)){
//hide text view
}
}
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.
So, you're calling startActivity twice, once with the data and once with a new Intent which will not have any data..
Protect from NPE
final Intent intent = getIntent();
if (intent != null) {
...
}
else {
Log.d(TAG, "No intent");
}
TRY This out in the showdata class.
public String barcode = null;
Intent i = getIntent();
barcode =i.getStringExtra("barccode");
Following this solution I have run into an issue with the code presented. The app forces a close every time I pick a contact from the list that pops up. Something tells me the culprit has something to do with this which I found in logcat.
Here is my version of the code:
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (1) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor cursor = managedQuery(contactData, null, null, null, null);
cursor.moveToFirst();
String name = cursor.getString(cursor.getColumnIndexOrThrow(Contacts.People.NAME));
String number = cursor.getString(cursor.getColumnIndexOrThrow(Contacts.People.NUMBER));
name1.setText(name);
num1.setText(number);
}
break;
}
}
Take a look at the logcat