Android 无法解析构造函数 - 静态片段
Android cannot resolve constructor - static Fragment
andorid 的新手,我一直在尝试在片段中引用 'this'。
使用 Navigation Draw 模板项目,它有一个静态的 class 作为主 Fragment。
我正在尝试集成 zxing 条形码扫描仪,它需要引用 Fragment 的目的,但是在使用 this
时出错,说它无法解析构造函数。
我认为是由于 class 的静态性质,但不确定如何解决它。
我试过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();
}
}
}
ZXing 使用 app.Fragments
,模板项目使用 support.v4.app.Fragment
。
Google 鼓励使用支持片段,因为它们会定期修复错误,但这完全取决于您的图书馆的决定。
andorid 的新手,我一直在尝试在片段中引用 'this'。
使用 Navigation Draw 模板项目,它有一个静态的 class 作为主 Fragment。
我正在尝试集成 zxing 条形码扫描仪,它需要引用 Fragment 的目的,但是在使用 this
时出错,说它无法解析构造函数。
我认为是由于 class 的静态性质,但不确定如何解决它。
我试过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();
}
}
}
ZXing 使用 app.Fragments
,模板项目使用 support.v4.app.Fragment
。
Google 鼓励使用支持片段,因为它们会定期修复错误,但这完全取决于您的图书馆的决定。