0

我正在尝试使用以下代码通过 zxing 从 QR 码中提取数据(基本上是从教程中复制粘贴,更改名称以保护无辜者):

            //Check for Barcode scanner, if not found put up an alert that allows user to install it.
            PackageManager pm = getPackageManager();
            try {
                ApplicationInfo appInfo = pm.getApplicationInfo("com.google.zxing.client.android", 0);
                Intent intent = new Intent(
                "com.google.zxing.client.android.SCAN");
                intent.putExtra("SCAN_MODE", "QR_CODE_MODE");//for Qr code, its "QR_CODE_MODE" instead of "PRODUCT_MODE"
                intent.putExtra("SAVE_HISTORY", false);//this stops saving ur barcode in barcode scanner app's history
                startActivityForResult(intent, 0);
            } catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                 new AlertDialog.Builder(BitcoinAddressUtilityActivity.this)
                    .setTitle("WARNING:")
                    .setMessage("You don't have Barcode Scanner installed. Please install it.")
                     .setCancelable(false)
                    .setNeutralButton("Install it now", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {         
                                  Uri uri = Uri.parse("market://search?q=pname:com.google.zxing.client.android");
                                  startActivity(new Intent(Intent.ACTION_VIEW, uri));
                            }
                    })
                    .show();

            }


        }

        public void onActivityResult(int requestCode, int resultCode, Intent intent) {
               if (requestCode == 0) {
                  if (resultCode == RESULT_OK) {
                     String contents = intent.getStringExtra("SCAN_RESULT");
                     String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                     // Handle successful scan
                     EditText passphrase = (EditText) findViewById(R.id.txtPassphrase);
                     passphrase.setText(contents);
                  } else if (resultCode == RESULT_CANCELED) {
                     // Handle cancel
                  }
               }
            }


    });

它执行得很好,没有错误,但 onActivityResults 永远不会触发,我什至收到警告:

The method onActivityResult(int, int, Intent) from the type new View.OnClickListener(){} is never used locally

我完全承认我是一个活动/意图菜鸟,所以如果有人想以教程的形式回答,我很乐意接受——我不只是想要修复代码,我想知道为什么会这样一开始不工作。

4

1 回答 1

2

您的代码中有以下问题

public void onActivityResult(int requestCode, int resultCode, Intent intent)您已经在按钮 onclick 侦听器中编写了方法。把它移到外面。

你的代码片段应该是这样的

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

            ----
            ----
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if (requestCode == 0) {
              if (resultCode == RESULT_OK) {
                 String contents = intent.getStringExtra("SCAN_RESULT");
                 String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                 // Handle successful scan
                 EditText passphrase = (EditText) findViewById(R.id.txtPassphrase);
                 passphrase.setText(contents);
              } else if (resultCode == RESULT_CANCELED) {
                 // Handle cancel
              }
           }

}
于 2012-06-10T06:27:12.540 回答