Skip to content

Commit

Permalink
TeamNewPipe#6522: Fix null pointer exception when displaying SearchF…
Browse files Browse the repository at this point in the history
…ragment

It seems due to TeamNewPipe#6394 updating the FragmentX library there was a
change to the order of lifecycle calls, as such onResume() was no longer
before onCreateOptionsMenu() creating a null pointer exception when
using service in onCreateOptionsMenu() as it is only set in onResume().

By moving the initialization of service to onStart() which still happens
before onCreateOptionsMenu() this crash can be avoided. This commit also
adds a check for a null service to prevent future crashes for similar
issues.
  • Loading branch information
Douile committed Jun 22, 2021
1 parent 62b4f33 commit 384ca66
Showing 1 changed file with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,25 @@ public void onViewCreated(@NonNull final View rootView, final Bundle savedInstan
initSearchListeners();
}

private void updateService() {
try {
service = NewPipe.getService(serviceId);
} catch (final Exception e) {
ErrorActivity.reportUiErrorInSnackbar(this,
"Getting service for id " + serviceId, e);
}
}

@Override
public void onStart() {
if (DEBUG) {
Log.d(TAG, "onStart() called");
}
super.onStart();

updateService();
}

@Override
public void onPause() {
super.onPause();
Expand All @@ -250,13 +269,6 @@ public void onResume() {
}
super.onResume();

try {
service = NewPipe.getService(serviceId);
} catch (final Exception e) {
ErrorActivity.reportUiErrorInSnackbar(this,
"Getting service for id " + serviceId, e);
}

if (suggestionDisposable == null || suggestionDisposable.isDisposed()) {
initSuggestionObserver();
}
Expand Down Expand Up @@ -428,6 +440,12 @@ public void onCreateOptionsMenu(@NonNull final Menu menu,
int itemId = 0;
boolean isFirstItem = true;
final Context c = getContext();

if (service == null) {
Log.w(TAG, "onCreateOptionsMenu() called with null service");
updateService();
}

for (final String filter : service.getSearchQHFactory().getAvailableContentFilter()) {
if (filter.equals(YoutubeSearchQueryHandlerFactory.MUSIC_SONGS)) {
final MenuItem musicItem = menu.add(2,
Expand Down

0 comments on commit 384ca66

Please sign in to comment.