我的申请中有两项活动。一种活动很简单,没有片段,另一种是父级活动,具有多个片段。在一个片段上,我有一个按钮,单击该按钮,应用程序将切换到下一个活动。现在,我在第二个活动中执行一些任务,然后切换回当前片段,但由于找不到片段视图而崩溃。我尝试了各种解决方案,但无法解决。请帮忙!
从第二个活动切换到片段的代码:
SearchProductFragment fragment = new SearchProductFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame, fragment);
transaction.commit();
要进行第二次活动的片段代码:
Intent in = new Intent(getActivity(), ScanActivity.class);
startActivity(in);
第二活动代码:
public class ScanActivity extends AppCompatActivity implements ZBarScannerView.ResultHandler {
private ZBarScannerView mScannerView;
private static final int REQUEST_CAMERA = 1;
ArrayList<ModelProductDetail> modelProductArticleCodeList;
DatabaseHelper databaseHelper;
String article_code;
ArrayList<ModelUnrecognisedCode> modelUnrecognisedCodeArrayList;
ArrayList<ModelUnrecognisedCode> singleUnrecognisedCheck;
private static FragmentManager fragmentManager;
//camera permission is needed.
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
databaseHelper = new DatabaseHelper(this);
fragmentManager = getSupportFragmentManager();//Get Fragment Manager
checkUserPermission();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (mScannerView == null) {
mScannerView = new ZBarScannerView(this); // Programmatically initialize the scanner view
setContentView(mScannerView);
}
} else {
Toast.makeText(this, R.string.permission_denied, Toast.LENGTH_SHORT).show();
checkUserPermission();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void checkUserPermission() {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, 1);
return;
}
}
loadImage();
}
private void loadImage() {
if (mScannerView == null) {
mScannerView = new ZBarScannerView(this); // Programmatically initialize the scanner view
setContentView(mScannerView);
}
}
@Override
public void onResume() {
super.onResume();
if (mScannerView instanceof ZBarScannerView) {
mScannerView.startCamera();
mScannerView.setResultHandler(this);
// Stop camera on pause
}
}
@Override
public void onPause() {
super.onPause();
if (mScannerView instanceof ZBarScannerView) {
mScannerView.stopCamera(); // Stop camera on pause
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mScannerView instanceof ZBarScannerView) {
mScannerView.stopCamera(); // Stop camera on pause
}
}
@Override
public void handleResult(me.dm7.barcodescanner.zbar.Result result) {
String resultCode = result.getContents();
Log.e("TAG", "handleResult: " + resultCode);
mScannerView.stopCamera();
modelProductArticleCodeList = new ArrayList<>();
modelProductArticleCodeList = databaseHelper.getProductByArtCode(resultCode);
if (modelProductArticleCodeList.size() == 0) {
Vibrator vibrator = (Vibrator) getApplicationContext().getSystemService(getApplicationContext().VIBRATOR_SERVICE);
vibrator.vibrate(1000);
singleUnrecognisedCheck = new ArrayList<>();
singleUnrecognisedCheck = databaseHelper.getUnrecognisedByCode(resultCode);
if (singleUnrecognisedCheck.size() == 0) {
Intent in = new Intent(this, ContainAllFragmentsActivity.class);
in.putExtra("unrecognised_alert", true);
in.putExtra("unrecognised_code", resultCode);
in.putExtra("jump", "1");
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(in);
} else {
Toast.makeText(this, R.string.unrecognised_code_has_been_added, Toast.LENGTH_SHORT).show();
Intent in = new Intent(this, ContainAllFragmentsActivity.class);
in.putExtra("jump", "3");
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(in);
}
} else {
Intent in = new Intent(this, ContainAllFragmentsActivity.class);
in.putExtra("product_art", resultCode);
in.putExtra("source_type", "art");
in.putExtra("jump", "2");
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(in);
}
mScannerView.resumeCameraPreview(this);
}
public void changeFragments(Fragment fragment) {
FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
private void getDialog() {
final Dialog d = new Dialog(this);
d.setContentView(R.layout.dialog_unrecognised_code);
d.setCanceledOnTouchOutside(true);
d.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
final EditText etCode = (EditText) d.findViewById(R.id.etCode);
final EditText etrecognisedComment = (EditText) d.findViewById(R.id.etrecognisedComment);
ImageView imgClose = (ImageView) d.findViewById(R.id.imgClose);
ImageButton ibSubmit = (ImageButton) d.findViewById(R.id.ibSubmit);
imgClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
d.dismiss();
}
});
ibSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ModelUnrecognisedCode modelUnrecognisedCode = new ModelUnrecognisedCode();
modelUnrecognisedCode.setUnrecognised_code(etCode.getText().toString());
modelUnrecognisedCode.setUnrecognised_comment(etrecognisedComment.getText().toString());
databaseHelper.addUnrecognisedCode(modelUnrecognisedCode);
// changeFragments(new SearchProductFragment());
modelUnrecognisedCodeArrayList = new ArrayList<>();
modelUnrecognisedCodeArrayList = databaseHelper.getUnrecognisedCode();
Log.e("TAG", "unrecognised code: " + modelUnrecognisedCodeArrayList.size());
d.dismiss();
}
});
d.show();
}
}