Android条形码(按下后退按钮时崩溃)

时间:2017-04-21 14:17:47

标签: java android

每当我按扫描按钮扫描条形码时,按下后退按钮时,它就会崩溃。我想在按下后退按钮时进行,它将恢复主要活动。任何人都可以知道我的代码有什么问题吗?

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                                                                                  处理:in.aurora.android_barcode_scanner,PID:9700                                                                                  java.lang.RuntimeException:将结果ResultInfo {who = null,request = 49374,result = 0,data = null}传递给activity {in.aurora.android_barcode_scanner / in.aurora.android_barcode_scanner.HomeActivity}:java.lang.NullPointerException :尝试在空对象引用上调用虚方法'boolean java.lang.String.equals(java.lang.Object)'                                                                                      在android.app.ActivityThread.deliverResults(ActivityThread.java:4089)                                                                                      在android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)                                                                                      在android.app.ActivityThread.-wrap20(ActivityThread.java)                                                                                      在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1533)                                                                                      在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                      在android.os.Looper.loop(Looper.java:154)                                                                                      在android.app.ActivityThread.main(ActivityThread.java:6119)                                                                                      at java.lang.reflect.Method.invoke(Native Method)                                                                                      在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)                                                                                   引发者:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'boolean java.lang.String.equals(java.lang.Object)'                                                                                      在in.aurora.android_barcode_scanner.HomeActivity.price(HomeActivity.java:113)                                                                                      在in.aurora.android_barcode_scanner.HomeActivity.onActivityResult(HomeActivity.java:57)                                                                                      在android.app.Activity.dispatchActivityResult(Activity.java:6932)                                                                                      在android.app.ActivityThread.deliverResults(ActivityThread.java:4085)                                                                                      在android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)                                                                                      在android.app.ActivityThread.-wrap20(ActivityThread.java)                                                                                      在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1533)                                                                                      在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                      在android.os.Looper.loop(Looper.java:154)                                                                                      在android.app.ActivityThread.main(ActivityThread.java:6119)                                                                                      at java.lang.reflect.Method.invoke(Native Method)                                                                                      在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

1 个答案:

答案 0 :(得分:2)

在为其分配值之前,您似乎正在使用codeContent

//we have a relult
price(codeContent);
codeContent = scanningResult.getContents();

您可以翻转这些行:

//we have a relult
codeContent = scanningResult.getContents();
price(codeContent);

实际上异常堆栈跟踪正好告诉你:

...
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:112)

这意味着price()试图检查字符串相等性但它引发了异常,因为引用为空