按下后退后,扫描条形码方法无效

时间:2018-06-11 09:07:26

标签: android barcode

我正在CIPHERLAB设备上开发一个应用程序,该应用程序有两个激活激光的横向物理按钮,可能会留下一些条形码。

我的主要问题是我会让它正常工作,但如果我通过按下后退按钮返回到另一个活动,并返回到我可以扫描条形码的同一活动,如果我扫描另一个代码,当应用程序崩溃我回到那个活动,它给出了以下错误,如果我扫描代码就会崩溃:

  

06-11 12:11:47.211 25170-25170 / com.example.igardini.visualposmobile E / AndroidRuntime:FATAL EXCEPTION:main       处理:com.example.igardini.visualposmobile,PID:25170       android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.os.BinderProxy@89ed74e无效;你的活动在运行吗?           在android.view.ViewRootImpl.setView(ViewRootImpl.java:796)           在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:351)           在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)           在android.app.Dialog.show(Dialog.java:322)           在com.example.igardini.visualposmobile.cassa.CustomAllert(cassa.java:217)           在com.example.igardini.visualposmobile.cassa $ 2.run(cassa.java:123)           在android.os.Handler.handleCallback(Handler.java:836)           在android.os.Handler.dispatchMessage(Handler.java:103)           在android.os.Looper.loop(Looper.java:203)           在android.app.ActivityThread.main(ActivityThread.java:6251)           at java.lang.reflect.Method.invoke(Native Method)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1063)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

这是我的cassa.java活动代码,其中我实现了用于使用激光扫描条形码并将其打印为字符串的方法。

     @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cassa);

    editText = findViewById(R.id.editTextBar);

    myDB = new DataBaseHandler(this);

    itemCassas = new ArrayList<>();
    buildTopRecyclerView();

     loadData();
     buildRecyclerView();


    price = findViewById(R.id.priceText);


    m_RM = ReaderManager.InitInstance(getApplicationContext());
    mReaderCallback = cassa.this;

    IntentFilter filter = new IntentFilter();
    filter.addAction(GeneralString.Intent_READERSERVICE_CONNECTED);
    registerReceiver(myDataReceiver, filter);


}


@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(myDataReceiver);
}

private final BroadcastReceiver myDataReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(GeneralString.Intent_READERSERVICE_CONNECTED)) {

            if(mReaderCallback != null){
                m_RM.SetReaderCallback(mReaderCallback);
            }

        }
    }
};



// getting a string of barcode after scanning it

@Override
public void onDecodeComplete(final String arg0) throws RemoteException {
    this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            editText.setText(arg0);

            // using the string got from the scanning for searching some data from the DB

            if(!TextUtils.isEmpty(editText.getText().toString())) {
                searchKeyword = editText.getText().toString();
                result = myDB.dbRawSearch(searchKeyword);

                if(result != null) {
                    String[] split = result.split("\n");

                    if (!split[0].equals("")) {
                        resultArt = myDB.dbRawArticoli(split[0]);

                        String[] splitArt = resultArt.split("\n");

                        itemCassas.add(new ItemCassa(splitArt[0], splitArt[1], Double.parseDouble(splitArt[2].replace(",", "."))));
                        exampleAdapter.notifyItemInserted(itemCassas.size());
                    } else {
                        CustomAllert();
                    }
                }
            }
        }
    });
}

@Override
public IBinder asBinder() {
    return null;
}


// Funzione per il controllo del pulsante Back
@Override
public void onBackPressed() {
    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    super.onBackPressed();

}

public void CustomAllert(){
    AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);

    @SuppressLint("InflateParams") View mView = getLayoutInflater().inflate(R.layout.alert_dialog, null);

    Button button = mView.findViewById(R.id.btnOK);

    mBuilder.setView(mView);
    final AlertDialog dialog = mBuilder.create();
    dialog.show();
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();

        }
    });


}

0 个答案:

没有答案