Whenever I press the scan button for scanning the barcode, when I press the back button, it crashed . I want to make it when the back button is pressed, it will resume back the main activity. Can anyone know what's the problem for my code ?
HomeActivity.java
public class HomeActivity extends ActionBarActivity {
public String codeFormat,codeContent;
public TextView formatTxt, contentTxt, price, productName, productDate;
public String name,date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
formatTxt = (TextView)findViewById(R.id.scan_format);
contentTxt = (TextView)findViewById(R.id.scan_content);
price = (TextView)findViewById(R.id.scan_price);
productName = (TextView)findViewById(R.id.scan_name);
productDate = (TextView)findViewById(R.id.scan_date);
}// end HomeActivity class
public void scanNow(View view){
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
integrator.setPrompt("Scan a Bar Code");
integrator.setResultDisplayDuration(0);
integrator.setWide(); // Wide scanning rectangle, may work better for 1D barcodes
integrator.setCameraId(0); // Use a specific camera of the device
integrator.initiateScan();
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//retrieve scan result
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
//we have a result
price(codeContent);
codeContent = scanningResult.getContents();
codeFormat = scanningResult.getFormatName();
// display it on screen
formatTxt.setText("FORMAT: " + codeFormat);
contentTxt.setText("CONTENT: " + codeContent);
price.setText("PRICE: " + "RM" + price(codeContent) );
productName.setText("ITEM: " + name);
productDate.setText("EXPIRY DATE: " + date);
Button addButton = (Button)findViewById(R.id.inventory_add);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
builder.setTitle("");
builder.setMessage("Add this product to inventory? ");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
}
});
builder.setNegativeButton("No",null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
}else{
Toast toast = Toast.makeText(getApplicationContext(),"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
public double price( String codeContent )
{
final String array[] = {"9789834508654","9556404116409"};
final String productName[] = {"MATHEMATICS for MATRICULATION SEMESTER 1 . THIRD EDITION",};
final String productDate[] = {"", "13/1/2018"};
final double price1[] = {50.00,1.60};
double price = 0;
for (int x = 0; x < array.length; x++ )
{
if(codeContent.equals(array[x]))
{
price = price1[x];
name = productName[x];
date = productDate[x];
break;
}
}
return price;
}
}
--------- beginning of crash
04-21 15:02:20.190 9700-9700/in.aurora.android_barcode_scanner E/AndroidRuntime: FATAL EXCEPTION: main Process: in.aurora.android_barcode_scanner, PID: 9700 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=49374, result=0, data=null} to activity {in.aurora.android_barcode_scanner/in.aurora.android_barcode_scanner.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at android.app.ActivityThread.deliverResults(ActivityThread.java:4089) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132) at android.app.ActivityThread.-wrap20(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at in.aurora.android_barcode_scanner.HomeActivity.price(HomeActivity.java:113) at in.aurora.android_barcode_scanner.HomeActivity.onActivityResult(HomeActivity.java:57) at android.app.Activity.dispatchActivityResult(Activity.java:6932) at android.app.ActivityThread.deliverResults(ActivityThread.java:4085) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132) at android.app.ActivityThread.-wrap20(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)