I check out similar question but no one helped me
I have 1 activity and 2 fragments inside it
1st fragment contains list of movies, 2nd - qr-scanner
if I scan the QR-code, fragment changed by themself and list-fragment returns foreground
if QR-code containes new movie - it will be add to list(it works ok)
if QR-code containes already exist movie - I need to show snackbar
my issue - while I try to show snackbar app crashes with
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.appolinary.msapphometask, PID: 20992
java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view.
at com.google.android.material.snackbar.Snackbar.make(Snackbar.java:181)
at com.appolinary.msapphometask.presentation.view.AllMoviesFragment.showSnackbar(AllMoviesFragment.java:113)
QR scanner fragment
public class QRReaderFragment extends Fragment {
private final String TAG = "MSApp";
private BarcodeView barcodeView;
private final int PERMISSIONS_REQUEST_ACCESS_CAMERA = 0;
private View view;
private OnQRReaderListener qrReaderListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_qrreader, container, false);
if (Objects.requireNonNull(getActivity()).checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
PERMISSIONS_REQUEST_ACCESS_CAMERA);
} else {
barcodeView = view.findViewById(R.id.barcode_scanner);
barcodeView.decodeContinuous(callback);
}
qrReaderListener = (OnQRReaderListener) getActivity();
return view;
}
private BarcodeCallback callback = new BarcodeCallback() {
@Override//TODO need to check bar-code for validity and prevent crash of application
public void barcodeResult(BarcodeResult result) {
if (result.getText() != null) {
barcodeView.pause();
String tag_string = result.getText();
MovieDetails movie = new Gson().fromJson(tag_string, MovieDetails.class);
returnToMovieList(movie);
Log.d(TAG,"resuming scanner, data needed was not found");
barcodeView.resume();
}
}
}
};
private void returnToMovieList(MovieDetails movie) {
qrReaderListener.dataReceived(movie);
FragmentManager fm = getActivity().getSupportFragmentManager();
fm.popBackStack();
}
public interface OnQRReaderListener{
void dataReceived(MovieDetails movie);
}
}
list-fragment
public class AllMoviesFragment extends Fragment implements RecyclerViewAdapter.OnRecyclerViewItemClickListener,
MovieListActivity.SnackbarLauncher {
private static final String CURR_MOVIE = "Current Movie";
private static final String TAG = "MSApp";
View resultView;
RecyclerView recyclerView;
Button qrButton;// will lead us to QR Scanner
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
resultView = inflater.inflate(R.layout.fragment_all_movies, container, false);
recyclerView = resultView.findViewById(R.id.recycler_view);
qrButton = resultView.findViewById(R.id.button_qr);
qrButton.setOnClickListener(v -> {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
Fragment qrScannerFragment = new QRReaderFragment();
String tag = "QRScanner";
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_container, qrScannerFragment, tag);
transaction.addToBackStack(tag);
transaction.commit();
});
@Override
public void showSnackbar() {
Log.d(TAG, "showSnackbar: this.getView() == null ? " + (this.getView()));
Log.d(TAG, "showSnackbar: resultView == null ? " + (resultView == null));
final LinearLayout linearLayout = resultView.findViewById(R.id.fragment_all_movies);
View view = resultView.findViewById(android.R.id.content);
Snackbar.make(view, getResources().getString(R.string.movie_exists), Snackbar.LENGTH_LONG);
}
}
activity
public class MovieListActivity extends AppCompatActivity implements QRReaderFragment.OnQRReaderListener {
String tag = "MovieList";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movie_list);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment allMoviesFragment = new AllMoviesFragment();
transaction.replace(R.id.main_container, allMoviesFragment, tag);
transaction.commit();
}
@Override
public void dataReceived(MovieDetails movie) {
Toast.makeText(this, movie.toString(), Toast.LENGTH_LONG).show();
DatabaseHelper db = App.getAppInstance().getDatabaseInstance();
MovieDetails tempMovie = db.getMovieDao().getMovieByName(movie.getTitle());
if (tempMovie == null) {
Log.d(TAG, "dataReceived: point1");
db.getMovieDao().insert(movie);
} else {//comparing only by title here!!!
FragmentManager fragmentManager = getSupportFragmentManager();
AllMoviesFragment allMoviesFragment = (AllMoviesFragment) fragmentManager.findFragmentByTag(tag);
allMoviesFragment.showSnackbar();
}
}
}
it crashes in the line
Snackbar.make(view, getResources().getString(R.string.movie_exists), Snackbar.LENGTH_LONG);
I tried to use resultView(from onCreateView method), I tried to use findViewById(android.R.id.content) - all of them are not null but no success