谁能告诉我如何正确地将我的应用程序上下文传递给我的按钮 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);