有人能告诉我如何将我的应用程序上下文正确传递给我的按钮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 参数,如下所示:
Context c = getApplicationContext();
IntentIntegrator integrator = new IntentIntegrator(c);
答案 0 :(得分:10)
尝试替换
Context c = getApplicationContext();
通过
Context c = ScaningActivity.this;
答案 1 :(得分:0)
对于Context
,您需要发送您的活动名称或<:p>
IntentIntegrator integrator = new IntentIntegrator(Your_Activity_name Or this);