我的代码出了什么问题。我收到错误"找不到符号方法findViewById(int)"。 Fragment是新的包。没有按钮代码部分,它可以很好地工作。
public class Scanner1 extends Fragment {
private Button scannerButton;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.scan_main,container,false);
return rootView;
scannerButton = (Button) findViewById(R.id.scannerButton);
scannerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), BarcodeScanner.class);
startActivity(intent);
}
});
}
答案 0 :(得分:0)
片段没有findViewById。您必须使用rootView
public class Scanner1 extends Fragment {
private Button scannerButton;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.scan_main,container,false);
scannerButton = (Button)rootView.findViewById(R.id.scannerButton);
scannerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), BarcodeScanner.class);
startActivity(intent);
}
});
return rootView;
}
}