-2
public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    ProductDatabase.ProductDatabaseHelper controller = new ProductDatabase.ProductDatabaseHelper(this);
    EditText mBarcodeEdit;
    EditText mFormatEdit;
    EditText mTitleEdit;
    EditText mPriceEdit;
    private static final int ZBAR_SCANNER_REQUEST = 0;
    private static final int ZBAR_QR_SCANNER_REQUEST = 1;
    private static final ProductData mProductData = new ProductData();
    Button mScanButton;
    Button mAddButton;
    Button mSelectButton;
    Button mExportButton;
    Button btnimport;
    ProductDatabase mProductDb;
    ListView ls;
    TextView infotext;
    File file = null;
//    DatabaseHelper dbhelper = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ls = (ListView) findViewById(R.id.placeslist);
        mBarcodeEdit = (EditText) findViewById(R.id.barcodeEdit);
        mFormatEdit = (EditText) findViewById(R.id.codeFormatEdit);
        mTitleEdit = (EditText) findViewById(R.id.titleEdit);
        mPriceEdit = (EditText) findViewById(R.id.priceEdit);
        mScanButton = (Button) findViewById(R.id.scan_btn);
        mAddButton = (Button) findViewById(R.id.addButton);
        mSelectButton = (Button) findViewById(R.id.selelctButton);
        mProductDb = new ProductDatabase(this); // not yet shown
        infotext = (TextView) findViewById(R.id.txtresulttext);
        mExportButton = (Button) findViewById(R.id.exportbtn);



        mSelectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, product_list.class);
                startActivity(i);
            }

        });
        mAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String barcode = mBarcodeEdit.getText().toString();
                String format = mFormatEdit.getText().toString();
                String title = mTitleEdit.getText().toString();
                String price = mPriceEdit.getText().toString();

                String errors = validateFields(barcode, format, title, price);
                if (errors.length() > 0) {
                    showInfoDialog(MainActivity.this, "Please fix errors", errors);
                } else {
                    mProductData.barcode = barcode;
                    mProductData.format = format;
                    mProductData.title = title;
                    mProductData.price = new BigDecimal(price);

                    mProductDb.insert(mProductData);
                    showInfoDialog(MainActivity.this, "Success", "Product saved successfully");
                    resetForm();
                }

            }
        });


        mExportButton.setOnClickListener(new View.OnClickListener() {
            SQLiteDatabase sqldb = controller.getReadableDatabase(); //My Database class
            Cursor c =null;
            @Override
            public void onClick(View view) { //main code begins here
                try {
                    c = sqldb.rawQuery("select * from spot_pay.db", null);

                    int rowcount = 0;
                    int colcount = 0;
                    File sdCardDir = Environment.getExternalStorageDirectory();
                    String filename = "MyBackUp.csv";
                    // the name of the file to export with
                    File saveFile = new File(sdCardDir, filename);
                    FileWriter fw = new FileWriter(saveFile);

                    BufferedWriter bw = new BufferedWriter(fw);
                    rowcount = c.getCount();
                    colcount = c.getColumnCount();
                    if (rowcount > 0) {
                        c.moveToFirst();

                        for (int i = 0; i < colcount; i++) {
                            if (i != colcount - 1) {

                                bw.write(c.getColumnName(i) + ",");

                            } else {

                                bw.write(c.getColumnName(i));

                            }
                        }
                        bw.newLine();

                        for (int i = 0; i < rowcount; i++) {
                            c.moveToPosition(i);

                            for (int j = 0; j < colcount; j++) {
                                if (j != colcount - 1)
                                    bw.write(c.getString(j) + ",");
                                else
                                    bw.write(c.getString(j));
                            }
                            bw.newLine();
                        }
                        bw.flush();
                        infotext.setText("Exported Successfully.");
                    }
                } catch (Exception ex) {
                    if (sqldb.isOpen()) {
                        sqldb.close();
                        infotext.setText(ex.getMessage().toString());
                    }

                } finally {

                }

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }





    private void showInfoDialog(Context context, String title, String information) {
        new AlertDialog.Builder(context)
                .setMessage(information)
                .setTitle(title)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                }).show();
    }

    private void resetForm() {
        // TODO Auto-generated method stub
        mBarcodeEdit.getText().clear();
        mFormatEdit.getText().clear();
        mTitleEdit.getText().clear();
        mPriceEdit.getText().clear();
    }


    public void launchScanner(View v) {
        if (isCameraAvailable()) {
            Intent intent = new Intent(this, ZBarScannerActivity.class);
            startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
        } else {
            Toast.makeText(this, "Rear Facing Camera Unavailable", Toast.LENGTH_SHORT).show();
        }
    }

    public void launchQRScanner(View v) {
        if (isCameraAvailable()) {
            Intent intent = new Intent(this, ZBarScannerActivity.class);
            intent.putExtra(ZBarConstants.SCAN_MODES, new int[]{Symbol.QRCODE});
            startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
        } else {
            Toast.makeText(this, "Rear Facing Camera Unavailable", Toast.LENGTH_SHORT).show();
        }
    }

    public boolean isCameraAvailable() {
        PackageManager pm = getPackageManager();
        return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ZBAR_SCANNER_REQUEST:
            case ZBAR_QR_SCANNER_REQUEST:
                if (resultCode == RESULT_OK) {
                    mBarcodeEdit.setText(data.getStringExtra(ZBarConstants.SCAN_RESULT));
//                    Toast.makeText(this, "Scan Result = " + data.getStringExtra(ZBarConstants.SCAN_RESULT), Toast.LENGTH_SHORT).show();
                } else if (resultCode == RESULT_CANCELED && data != null) {
                    String error = data.getStringExtra(ZBarConstants.ERROR_INFO);
                    if (!TextUtils.isEmpty(error)) {
                        Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
    }

    private static String validateFields(String barcode, String format,
                                         String title, String price) {
        StringBuilder errors = new StringBuilder();

        if (barcode.matches("^\\s*$")) {
            errors.append("Barcode required\n");
        }

        if (format.matches("^\\s*$")) {
            errors.append("Format required\n");
        }

        if (title.matches("^\\s*$")) {
            errors.append("Title required\n");
        }

        if (!price.matches("^-?\\d+(.\\d+)?$")) {
            errors.append("Need numeric price\n");
        }

        return errors.toString();
    }

}
noufalcep
  • 3,446
  • 15
  • 33
  • 51
Tang Weiming
  • 47
  • 2
  • 9
  • Hello @Tang and welcome to StackOverflow! To have a better chance of getting answers, you should provide: 1) a description (or a full traceback) of the error you get and, most importantly, 2) a description of what you tried, what you think is not working and what should your example do. Also, try to provide a concise, self contained example if possible. – Soravux Aug 30 '16 at 04:25

1 Answers1

0

I think string may be null somewhere. Please type something in textfield or check for null string.

JibinNajeeb
  • 784
  • 1
  • 10
  • 31