1

谁能告诉我如何正确地将我的应用程序上下文传递给我的按钮 setOnClickListener?

当我单击使用 Zxing 在我的布局代码中定义的“扫描按钮”时,我正在尝试扫描条形码。

以下是我的代码:

    public class ScaningActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
            }
        });//close upload clicklistener

    final Button scan = (Button)findViewById(R.id.scanButton);
        scan.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Context c = getApplicationContext();

                // TODO Auto-generated method stub
                IntentIntegrator integrator = new IntentIntegrator(c);
                integrator.initiateScan();
            }
        });

    }//close onCreate
        public void onActivityResult(int requestCode, int resultCode, Intent intent) {
          IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
          if (scanResult != null) {
            // handle scan result
            Log.e("DATA", "TESTING SCAN RESULT : " + scanResult.toString());
          }else{
              Log.d("DATA", "NO SCAN RESULTS : ");// + scanResult.getFormatName());//scanResult.getContents());
          }

        }
}//close ScaningActivity

问题是IntentIntegrator无法识别我提供的上下文参数,如下所示:

Context c = getApplicationContext();
IntentIntegrator integrator = new IntentIntegrator(c);
4

2 回答 2

11

尝试更换

Context c = getApplicationContext();

经过

Context c = ScaningActivity.this;
于 2012-06-11T20:48:36.957 回答
0

因为Context您需要发送 yourActivityname 或这个:

IntentIntegrator integrator = new IntentIntegrator(Your_Activity_name Or this);

于 2012-06-11T20:42:07.647 回答