andorid的新手,我试图在片段中引用'this'。
使用Navigation Draw模板项目,该项目具有主片段的静态类。
我正在尝试集成zxing条形码扫描程序,它需要引用Fragment作为intent,但是当使用this
时,它会错误地说它无法解析构造函数。
我认为由于该类的静态性质,但不确定如何解决它。
我已尝试this
和PlaceholderFragment.this
......
public static class PlaceholderFragment extends Fragment implements Button.OnClickListener {
private Button scanBtn;
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
scanBtn = (Button) view.findViewById(R.id.scan_btn);
scanBtn.setOnClickListener(this);
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
@Override
public void onClick(View v) {
// `this` here errors saying it cant find the constructor.
// Im trying to pass a reference to this fragment...
IntentIntegrator integrator = new IntentIntegrator( this );
integrator.initiateScan();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
Toast.makeText(getActivity().getApplicationContext(), "You scanned", Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:1)
ZXing使用app.Fragments
,而模板项目使用support.v4.app.Fragment
。
Google鼓励使用支持片段,因为它们会定期修复,但这完全取决于您的图书馆决定的内容。