Java Android条形码(按下后退按钮时崩溃)
每当我按下扫描按钮扫描条形码时,当我按下后退按钮时,条形码崩溃。我想当按下后退按钮时,它将恢复主要活动。有人知道我的代码有什么问题吗Java Android条形码(按下后退按钮时崩溃),java,android,Java,Android,每当我按下扫描按钮扫描条形码时,当我按下后退按钮时,条形码崩溃。我想当按下后退按钮时,它将恢复主要活动。有人知道我的代码有什么问题吗 HomeActivity.java public class HomeActivity extends ActionBarActivity { public String codeFormat,codeContent; public TextView formatTxt, contentTxt, price, productName, productDate;
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
HomeActivity.java
公共类HomeActivity扩展了ActionBarActivity{
公共字符串codeFormat,codeContent;
公共文本视图格式文本、内容文本、价格、产品名称、产品日期;
公共字符串名称、日期;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
formatTxt=(TextView)findViewById(R.id.scan\u格式);
contentTxt=(TextView)findViewById(R.id.scan\u content);
price=(TextView)findViewById(R.id.scan\u price);
productName=(TextView)findViewById(R.id.scan\u名称);
productDate=(TextView)findViewById(R.id.scan\u日期);
}//结束家庭活动课
public void scanNow(视图){
监控整合器=新的监控整合器(本);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE\u D\u代码类型);
integrator.setPrompt(“扫描条形码”);
积分器.setResultDisplayDuration(0);
integrator.setWide();//宽扫描矩形,可能更适合1D条形码
integrator.setCameraId(0);//使用设备的特定摄像头
integrator.initiateScan();
}
ActivityResult上的公共void(int请求代码、int结果代码、意图){
//检索扫描结果
IntentResult scanningResult=IntentIntegrator.parseActivityResult(请求代码、结果代码、意图);
如果(扫描结果!=null){
//我们有一个结果
价格(编码内容);
codeContent=scanningResult.getContents();
codeFormat=scanningResult.getFormatName();
//在屏幕上显示它
formatTxt.setText(“格式:”+code格式);
contentTxt.setText(“内容:+codeContent”);
price.setText(“价格:”+“RM”+价格(codeContent));
productName.setText(“项:+name”);
productDate.setText(“到期日:+日期”);
Button addButton=(按钮)findViewById(R.id.inventory\u add);
addButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(最终视图){
AlertDialog.Builder=新建AlertDialog.Builder(HomeActivity.this);
builder.setTitle(“”);
setMessage(“是否将此产品添加到库存?”);
builder.setPositiveButton(“是”,新建DialogInterface.OnClickListener(){
@凌驾
公共void onClick(对话框接口对话框,int i){
}
});
builder.setNegativeButton(“否”,空);
AlertDialog AlertDialog=builder.create();
alertDialog.show();
}
});
}否则{
Toast Toast=Toast.makeText(getApplicationContext(),“未收到扫描数据!”,Toast.LENGTH\u SHORT);
toast.show();
}
}
公共双价(字符串编码内容)
{
最终字符串数组[]={“9789834508654”,“9556404116409”};
最终字符串productName[]={“入学第一学期数学。第三版”,};
最终字符串productDate[]={“13/1/2018”};
最终双倍价格1[]={50.00,1.60};
双倍价格=0;
对于(int x=0;x
04-21 15:02:20.190 9700-9700/in.aurora.android_条形码_扫描仪E/AndroidRuntime:致命异常:main
进程:in.aurora.android_条形码_扫描器,PID:9700
java.lang.RuntimeException:未能将结果ResultInfo{who=null,request=49374,result=0,data=null}传递到活动{in.aurora.android\u barcode\u scanner/in.aurora.android\u barcode\u 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)
位于java.lang.reflect.Method.invoke(本机方法)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)上
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
//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)