我在主要活动(消息和扫描)中有一个带有2个按钮的应用程序点击扫描打开一个带有摄像头的片段。单击消息将替换扫描片段。直到这里一切都很完美。
然后当我想再次打开相机并点击扫描按钮时 没有反应。没有例外..直到我多次点击..然后相机片段打开。正确打开和关闭片段是否有任何问题?下面是点击和扫描按钮片段代码上的两个按钮的主要活动代码。任何帮助将不胜感激
public void message(View v)
{
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
message sms = new message();
scan scan = new scan();
if(fragmentManager.findFragmentByTag("scan")!=null) {
try
{
fragmentTransaction.replace(fragmentManager.findFragmentByTag("scan").getId(), sms, "text");
fragmentTransaction.commit();
Camera camera = null;
camera.release();
}
catch(Exception exe)
{
Toast.makeText(getApplicationContext(), exe.getMessage(),Toast.LENGTH_SHORT);
}
}
}
public void scan(View v)
{
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
scan scan = new scan();
if(fragmentManager.findFragmentByTag("text")!=null) {
try
{
fragmentTransaction.replace(fragmentManager.findFragmentByTag("text").getId(), scan, "scan");
fragmentTransaction.commit();
}
catch(Exception exe)
{
Toast.makeText(getApplicationContext(), exe.getMessage(),Toast.LENGTH_SHORT);
}
}
}
扫描按钮打开一个片段,这是该片段的java类
private OnFragmentInteractionListener mListener;
public scan() {
}
@Override
public void handleResult(Result rawResult) {
Log.e("handler", rawResult.getText());
Log.e("handler", rawResult.getBarcodeFormat().toString());
AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
builder.setTitle("Scan Result");
builder.setMessage(rawResult.getText());
number = rawResult.getText().substring(rawResult.getText().length() - 13);
final EditText input = new EditText(this.getActivity());
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
m_Text = input.getText().toString();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert1 = builder.create();
alert1.show();
zXingScannerView.resumeCameraPreview(this);
}
public static scan newInstance(String param1, String param2) {
scan fragment = new scan();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
if(checkCameraHardware(getActivity().getApplicationContext())) {
zXingScannerView = new ZXingScannerView(this.getActivity().getApplicationContext());
zXingScannerView.setResultHandler(this);
zXingScannerView.startCamera();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_scan, container, false);
FrameLayout preview =(FrameLayout)view.findViewById(R.id.camera_preview);
preview.addView(zXingScannerView);
return view;
}
@Override
public void onPause()
{
super.onPause();
zXingScannerView.stopCamera();
}
@Override
public void onResume() {
super.onResume();
zXingScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
zXingScannerView.startCamera(); // Start camera on resume
}
/** Check if this device has a camera */
public boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
if (mCamera!= null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}