简体   繁体   中英

Android app layout components are null when restoring from background after some time

The reason for the crash is a NullPointerException which is thrown because the fragment's UI Components are null for some reason.

Exception is thrown at this line:

private void setGameInfo(Game game) {
    // Stop/hide all ongoing progress bars (loading)
    mSummaryLoading.setVisibility(View.GONE);

}

Exception : java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference

This is my (very long) fragment code where the progress bar mSummaryLoading can be found

public class GameInfoFragment extends Fragment implements GamePageActivity.OnDataLoadedListener {


    public GameInfoFragment() {
        // Required empty public constructors
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_game_info, container, false);
        String dateFormat = SharedPrefManager.read(SharedPrefManager.KEY_PREF_DATE_FORMAT, getString(R.string.default_date_format));
        mDateFormatterGMT = new SimpleDateFormat(dateFormat);
        mDateFormatterGMT.setTimeZone(TimeZone.getTimeZone("GMT"));
        mTodayTimeMillis = Calendar.getInstance().getTimeInMillis();
        mDatabaseHelper = DatabaseHelper.getDatabaseHelper(getActivity());
        mAlarmReceiver = new AlarmReceiver();
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // onViewCreated make sure the view is fully created, called after onCreateView
        super.onViewCreated(view, savedInstanceState);
        // Summary UI
        mTextSummary = view.findViewById(R.id.summary);
        mReadMore = view.findViewById(R.id.read_more);
        mSummaryLoading = view.findViewById(R.id.summary_loading);
        // Platforms UI
        mPlatformsLayout = view.findViewById(R.id.platforms);
        mPlatformsLayoutExtra = view.findViewById(R.id.platforms_2);
        // Countdown UI
        mHeaderReleasesTitle = view.findViewById(R.id.header_count_down);
        mHeaderReleaseDate = view.findViewById(R.id.header_release_date);
        mCountdownLayout = view.findViewById(R.id.countdown_view);
        mOutNowTxt = view.findViewById(R.id.out_now_txt);
        mDaysTxt = view.findViewById(R.id.days_txt);
        mHoursText = view.findViewById(R.id.hours_txt);
        mMinutesText = view.findViewById(R.id.minutes_txt);
        mSecondsText = view.findViewById(R.id.seconds_txt);
        mCategoryText = view.findViewById(R.id.category_txt);
        mNoteTxt = view.findViewById(R.id.note_txt);
        // Release dates table UI
        mSpinnerRegions = view.findViewById(R.id.spinner_regions);
        mNoReleaseDatesTxt = view.findViewById(R.id.empty_releases);
        mReleaseTableLoading = view.findViewById(R.id.releases_loading);
        mTableReleaseDates = view.findViewById(R.id.table_release_dates);
        // Websites (Social link) UI
        mWebsitesList = view.findViewById(R.id.websitesList);
        mNoWebsitesTxt = view.findViewById(R.id.empty_websites_list);
        mSocialLoading = view.findViewById(R.id.social_loading);
        mWebsitesAdapter = new WebsitesAdapter(getContext());
        mWebsitesList.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        mWebsitesList.setAdapter(mWebsitesAdapter);
        // Start progressbar
        mSummaryLoading.setVisibility(View.VISIBLE);
        mSocialLoading.setVisibility(View.VISIBLE);
        mReleaseTableLoading.setVisibility(View.VISIBLE);
        // User region
        String region = SharedPrefManager.read(SharedPrefManager.KEY_PREF_REGION, "north_america");
        mRegion = SharedPrefManager.getRegionToFilter(region);
        // Release social actions
        // The RELEASE DATA OBJECT
        mRelease = ((GamePageActivity)getActivity()).getRelease();
        mAuth = FirebaseAuth.getInstance();
        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (firebaseAuth.getCurrentUser() != null) {
                    // When the user signs in in this page [refresh]
                   initHypes(mRelease.getId());
                }
            }
        };
        mReleasesActions = view.findViewById(R.id.release_actions);
        // Favorite
        mAddToFavorite = view.findViewById(R.id.favorite);
        mAddNote = view.findViewById(R.id.add_note);
        mEditAlarms = view.findViewById(R.id.alarms);
        mShareRelease = view.findViewById(R.id.share);
        mDatabaseHelper = DatabaseHelper.getDatabaseHelper(getActivity());
        // HYPE!!!
        mHypeLoading = view.findViewById(R.id.hype_loading);
        mHypeRelease = view.findViewById(R.id.hype);
        mHypeCountTxt = view.findViewById(R.id.hype_count);
        mHypeCountTxt.setText("0 hypes");
        // Release actions icons
        mStarIcon = view.findViewById(R.id.star_icon);
        mHeartIcon = view.findViewById(R.id.favorite_icon);
        mNoteIcon = view.findViewById(R.id.note_icon);
        mAlarmIcon = view.findViewById(R.id.alarm_icon);
        // Default colors [hype]
        mStarIcon.setImageResource(R.drawable.ic_star);
        mHeartIcon.setImageResource(R.drawable.ic_favorite);
        mNoteIcon.setImageResource(R.drawable.ic_note);
        mAlarmIcon.setImageResource(R.drawable.ic_notification);
        // Init likes (hypes) and puts color to yellow if liked
        // if game has't been released yet or has been released for a week
        if (mRelease.getDate() >= mTodayTimeMillis - 604800000) {
            initHypes(mRelease.getId());
            mHypeRelease.setVisibility(View.VISIBLE);
        } else {
            mHypeRelease.setVisibility(View.GONE);
        }
        // Social actions, favorite, alarm, note, etc.
        initReleaseActions();
        // Countdown
        initCountdown(mRelease);
        // Platforms
        initPlatforms(mRelease);
        // The release date
        mHeaderReleaseDate.setText(mDateFormatterGMT.format(mRelease.getDate()));
        // Expandable TextView
        mReadMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (mTextSummary.isExpanded()) {
                    mTextSummary.collapse();
                    mReadMore.setText("Read more");
                } else {
                    mTextSummary.expand();
                    mReadMore.setText("Collapse");
                }
            }
        });
        // Like [hype up] game
        mHypeRelease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAuth.getCurrentUser() != null) {
                    onStarClicked(mRelease, mAuth.getCurrentUser().getUid());
                } else {
                    startActivity(new Intent(getActivity(), SignInActivity.class));
                }
            }
        });
    }

    public void loadHypeLayout() {
        if (mHypeLoading.getVisibility() == View.VISIBLE) {
            mHypeLoading.setVisibility(View.GONE);
            mHypeCountTxt.setVisibility(View.VISIBLE);
            mStarIcon.setVisibility(View.VISIBLE);
        } else {
            mHypeLoading.setVisibility(View.VISIBLE);
            mHypeCountTxt.setVisibility(View.GONE);
            mStarIcon.setVisibility(View.GONE);
        }
    }

    public void initReleaseActions() {
        // User saved release actions
        if (mDatabaseHelper.favoriteExists(mRelease)) {
            mHeartIcon.setImageResource(R.drawable.ic_favorite_red);
            mAddNote.setVisibility(View.VISIBLE);
            // If game already out, can't have alarms
            if (mRelease.getDate() >= Calendar.getInstance().getTimeInMillis()) {
                mEditAlarms.setVisibility(View.VISIBLE);
            } else {
                mEditAlarms.setVisibility(View.GONE);
            }
        } else {
            mAddNote.setVisibility(View.GONE);
            mEditAlarms.setVisibility(View.GONE);
        }

        if (mDatabaseHelper.alarmExists(mRelease)) {
            mAlarmIcon.setImageResource(R.drawable.ic_notification_blue);
        }

        if (mDatabaseHelper.noteExists(mRelease)) {
            mNoteIcon.setImageResource(R.drawable.ic_note_red);
            mNoteTxt.setText("Edit note");
        }

        mAddToFavorite.setOnClickListener(new FavoriteOnClickListener(getContext(), mRelease, mAlarmIcon, mHeartIcon, mNoteIcon,
                                                                        mAddNote, mEditAlarms));

        mAddNote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddNoteFragment addNoteFragment = new AddNoteFragment();
                addNoteFragment.setDatabaseHelper(mDatabaseHelper);
                addNoteFragment.setRelease(mRelease);
                addNoteFragment.setIcon(mNoteIcon);
                addNoteFragment.setNoteTxt(mNoteTxt);
                addNoteFragment.show(getFragmentManager(), addNoteFragment.getTag());
            }
        });

        mEditAlarms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditAlarmsFragment editAlarmsFragment = new EditAlarmsFragment();
                editAlarmsFragment.setDatabaseHelper(mDatabaseHelper);
                editAlarmsFragment.setRelease(mRelease);
                editAlarmsFragment.setIcon(mAlarmIcon);
                editAlarmsFragment.show(getFragmentManager(), editAlarmsFragment.getTag());
            }
        });

        mShareRelease.setOnClickListener(new ShareOnClickListener(getContext(), mRelease, mAPISearchedGame));
    }

    public void initPlatforms(Release release) {
        List<Integer> platforms = release.getPlatforms();
        if (platforms != null) {
            for (int i = 0; i < platforms.size(); i++) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
                params.setMargins(0, 0, 18, 0);
                TextView current = new TextView(getActivity());
                current.setId(i + 1);
                current.setLayoutParams(params);
                current.setPadding(8, 8, 8, 8);
                current.setTextSize(14);
                current.setGravity(Gravity.CENTER);
                current.setTextColor(ContextCompat.getColor(getActivity(), R.color.color_text));
                switch (platforms.get(i)) {
                    case 6:
                        current.setText("Win");
                        current.setBackgroundResource(R.drawable.round_pc);
                        break;
                    case 49:
                        current.setText("Xbox One");
                        current.setBackgroundResource(R.drawable.round_xboxone);
                        break;
                    case 48:
                        current.setText("PS4");
                        current.setBackgroundResource(R.drawable.round_ps4);
                        break;
                    case 9:
                        current.setText("PS3");
                        current.setBackgroundResource(R.drawable.round_ps3);
                        break;
                    case 46:
                        current.setText("PS Vita");
                        current.setBackgroundResource(R.drawable.round_psvita);
                        break;
                    case 12:
                        current.setText("Xbox 360");
                        current.setBackgroundResource(R.drawable.round_xbox360);
                        break;
                    case 130:
                        current.setText("Nintendo Switch");
                        current.setBackgroundResource(R.drawable.round_switch);
                        break;
                    case 41:
                        current.setText("Wii U");
                        current.setBackgroundResource(R.drawable.round_wiiu);
                        break;
                    case 37:
                        current.setText("3DS");
                        current.setBackgroundResource(R.drawable.round_3ds);
                        break;
                    case 3:
                        current.setText("Linux");
                        current.setBackgroundResource(R.drawable.round_linux);
                        break;
                    case 14:
                        current.setText("Mac");
                        current.setBackgroundResource(R.drawable.round_mac);
                        break;
                    case 34:
                        current.setText("Android");
                        current.setBackgroundResource(R.drawable.round_android);
                        break;
                    case 39:
                        current.setText("IOS");
                        current.setBackgroundResource(R.drawable.round_ios);
                        break;
                    case 5:
                        current.setText("Wii");
                        current.setBackgroundResource(R.drawable.round_wii);
                        break;
                }
                // if (i == 7; change to platform_2)
                if (i < 7) {
                    mPlatformsLayout.addView(current);
                } else {
                    mPlatformsLayoutExtra.setVisibility(View.VISIBLE);
                    mPlatformsLayoutExtra.addView(current);
                }
            }
        }
    }

    public void initCountdown(Release release) {
        // https://stackoverflow.com/questions/46332398/android-countdown-based-on-gmt-utc-and-not-the-users-timezone
        // Set the release date in millis to calender set to GMT timezone (universal)
        Calendar releaseCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        releaseCalendar.setTimeInMillis(release.getDate());
        // Get the release date
        LocalDate localReleaseDate = null;

        boolean success = false;
        int dayOfMonth = releaseCalendar.get(Calendar.DAY_OF_MONTH);
        while (!success) {
            try {
                localReleaseDate = LocalDate.of(releaseCalendar.get(Calendar.YEAR),
                        releaseCalendar.get(Calendar.MONTH) + 1,
                        dayOfMonth);
                success = true;
            } catch (DateTimeException dateTimeException) {
                // Invalid date e.g 'November 31' Small fix?
                dayOfMonth -= 1;
            }
        }

        LocalTime localReleaseTime = LocalTime.of(0, 0, 0); // midnight release
        // Set to the user's timezone
        ZoneId zoneId = ZoneId.of(TimeZone.getDefault().getID());
        ZonedDateTime zonedDateTime = ZonedDateTime.of(localReleaseDate, localReleaseTime, zoneId);
        // Convert from a time zone to UTC. Same point on the timeline
        Instant instant = zonedDateTime.toInstant();
        Duration d = Duration.between(Instant.now(), instant);

        mCountdownTimer = new CountDownTimer(d.toMillis() , 1000) { // adjust the milli seconds here
            public void onTick(long millisUntilFinished) {
                // 1 sec = 1000 millis
                long seconds = millisUntilFinished / 1000;
                long minutes = seconds / 60;
                long hours = minutes / 60;
                long days = hours / 24;
                mDaysTxt.setText(String.valueOf(days));
                mHoursText.setText(String.valueOf(hours % 24));
                mMinutesText.setText(String.valueOf(minutes % 60));
                mSecondsText.setText(String.valueOf(seconds % 60));
            }

            public void onFinish() {
                mHeaderReleasesTitle.setText("Released on");
                mOutNowTxt.setVisibility(View.VISIBLE);
                mCountdownLayout.setVisibility(View.GONE);
                // Games already out, can't have alarms, but they can still be hyped for a week after release
                mEditAlarms.setVisibility(View.GONE);
            }
        };
        mCountdownTimer.start();

        long updatedAt = release.getUpdatedAt();
        if (updatedAt != 0) {
            mTxtLastUpdatedOnRelease.setText("Release date last updated on " + mDateFormatterGMT.format(updatedAt));
        }
    }

    private void setGameInfo(Game game) {
        // Stop/hide all ongoing progress bars (loading)
        mSummaryLoading.setVisibility(View.GONE);
        mSocialLoading.setVisibility(View.GONE);
        mReleaseTableLoading.setVisibility(View.GONE);
        loadHypeLayout();

        // The category
        String category = game.getCategory();
        if (category != null) {
            mCategoryText.setText(game.getCategory());
        }

        String summary = game.getSummary();
        if (summary != null) {
            mTextSummary.setText(summary);
        } else {
            mTextSummary.setText("No summary available");
            mReadMore.setVisibility(View.GONE);
        }

        List<Website> websites = game.getWebsites();
        if (websites != null) {
            mWebsitesAdapter.setData(websites);
        } else {
            mWebsitesList.setVisibility(View.GONE);
            mNoWebsitesTxt.setVisibility(View.VISIBLE);
        }

        // Organizing Table Release dates
        List<ReleaseDate> releaseDates = game.getReleaseDates();
        if (releaseDates != null) {
            // Creating the Hash map / Every region that exists will have a list of release dates
            mRegionsReleaseHashMap = new HashMap<>();
            for (ReleaseDate releaseDate : releaseDates) {
                String region = releaseDate.getRegionName();
                // Doesn't contain region
                if (!mRegionsReleaseHashMap.containsKey(region)) {
                    mRegionsReleaseHashMap.put(region, new ArrayList<ReleaseDate>());
                }
                mRegionsReleaseHashMap.get(region).add(releaseDate);
            }
            // Setting first the spinner, then the data
            if (!mRegionsReleaseHashMap.isEmpty()) {
                List<String> regions = new ArrayList<>();
                for (String region : mRegionsReleaseHashMap.keySet()) {
                    if (!regions.contains(region)) { regions.add(region); }
                }
                Collections.sort(regions); // By alpha order
                // Spinner takes an ArrayAdapter as adapter
                ArrayAdapter spinnerAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, regions);
                spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                mSpinnerRegions.setAdapter(spinnerAdapter);
                /*
                I removed this
                // First spinner item is always the user's region
                String defaultRegion = ((GamePageActivity) getActivity()).mRegion;
                if (regions.contains(defaultRegion)) {
                    mSpinnerRegions.setSelection(spinnerAdapter.getPosition(defaultRegion));
                } else if (regions.contains("Worldwide")) {
                    mSpinnerRegions.setSelection(spinnerAdapter.getPosition("Worldwide"));
                } */
                // Spinner listener [called on selected and on shown]
                mSpinnerRegions.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        initReleaseDatesTable(mRegionsReleaseHashMap.get(mSpinnerRegions.getSelectedItem().toString()));
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
            }
        } else {
            mNoReleaseDatesTxt.setVisibility(View.VISIBLE);
            mTableReleaseDates.setVisibility(View.GONE);
        }

        long updatedAt = game.getUpdatedAt();
        if (updatedAt != 0) {
            mTxtLastUpdatedOnGame.setText("Game data last updated on " + mDateFormatterGMT.format(updatedAt));
        }
    }

    public void initReleaseDatesTable(List<ReleaseDate> releaseDates) {
        mTableReleaseDates.removeAllViews();
        TableRow tblRowHeader = new TableRow(getContext());
        TextView txtPlatform = new TextView(getContext());
        txtPlatform.setTextSize(16);
        txtPlatform.setText("Platform");
        tblRowHeader.addView(txtPlatform);

        TextView txtReleaseDate = new TextView(getContext());
        txtReleaseDate.setTextSize(16);
        txtReleaseDate.setText("Release Date");
        tblRowHeader.addView(txtReleaseDate);

        TextView txtCountdown = new TextView(getContext());
        txtCountdown.setTextSize(16);
        txtCountdown.setText("Days left");
        tblRowHeader.addView(txtCountdown);

        mTableReleaseDates.addView(tblRowHeader);
        // Creating rows
        // One row: platform, release date and pre-order
        for (int i = 0; i < releaseDates.size(); i++) {
            TableRow tblRowData = new TableRow(getContext());

            TextView textPlatformData = new TextView(getContext());
            String platform = releaseDates.get(i).getPlatformName();
            // Gaming Reminder doesn't support/recognize this platform ;)
            if (!platform.isEmpty()) {
                textPlatformData.setTextSize(16);
                textPlatformData.setText(platform);
                tblRowData.addView(textPlatformData);

                TextView textReleaseDateData = new TextView(getContext());
                textReleaseDateData.setTextSize(16);
                long releaseTimeMillis = releaseDates.get(i).getDate();
                textReleaseDateData.setText(mDateFormatterGMT.format(releaseTimeMillis));
                tblRowData.addView(textReleaseDateData);

                TextView textCountdownData = new TextView(getContext());
                textCountdownData.setTextSize(16);
                long daysLeft = AppUtil.daysBetween(releaseTimeMillis);
                if (daysLeft <= 0) {
                    textCountdownData.setText("Now Out");
                } else {
                    textCountdownData.setText(daysLeft + " days");
                }
                tblRowData.addView(textCountdownData);
                mTableReleaseDates.addView(tblRowData);
            }
        }
    }

    // Shows the number of likes in our respective likes count view
    private void initHypes(final long releaseId) {
        // Toast.makeText(getContext(), "Loading likes...", Toast.LENGTH_SHORT).show();
        DatabaseReference likeRef = mLikesRef.child(mRegion);
        likeRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.child(String.valueOf(releaseId)).exists()) {
                    long starCount = dataSnapshot.child(String.valueOf(releaseId)).child("starCount").getValue(Long.class);
                    mHypeCountTxt.setText(starCount + " hypes");
                    // Now check if current logged in user hyped it (the release) up
                    if (mAuth.getCurrentUser() != null) {
                        // the stars node is only used to check if the current user hyped up the current game
                        if (dataSnapshot.child(String.valueOf(releaseId)).child("stars").hasChild(mAuth.getCurrentUser().getUid())) {
                            mStarIcon.setImageResource(R.drawable.ic_star_yellow);
                        } else {
                            mStarIcon.setImageResource(R.drawable.ic_star);
                        }
                    }
                } // else : no votes yet for this release
            }

            @Override
            public void onCancelled(DatabaseError databaseError) { }
        });
    }

    private void onStarClicked(final Release release, final String uid) {
        final DatabaseReference postRef = mLikesRef.child(mRegion).child(String.valueOf(release.getId()));
        postRef.runTransaction(new Transaction.Handler() {
            @Override
            public Transaction.Result doTransaction(MutableData mutableData) {
                Post p = mutableData.getValue(Post.class);
                if (p == null) {
                    // When p is null this means, the release object [no node exists] hasn't been added to likes
                    p = new Post();
                    p.setStarCount(1);
                    p.setRelease(release);
                    p.setDate(release.getDate());
                    p.getStars().put(uid, true);
                    mutableData.setValue(p);
                    return Transaction.success(mutableData);
                }

                if (p.getStars().containsKey(uid)) {
                    // Unstar the post and remove self from stars
                    p.setStarCount(p.getStarCount() - 1);
                    if (p.getStarCount() == 0) {
                        // Delete the node
                        postRef.removeValue();
                    }
                    // remove the entire key
                    p.getStars().remove(uid);
                } else {
                    // Star the post and add self to stars
                    p.setStarCount(p.getStarCount() + 1);
                    // Update the date [flawed, some release dates saved in likes can have a different release date]
                    // Update the release each time the user puts a star
                    p.setRelease(release);
                    p.getStars().put(uid, true);
                }

                // Set value and report transaction success
                mutableData.setValue(p);
                return Transaction.success(mutableData);
            }

            @Override
            public void onComplete(DatabaseError databaseError, boolean b,
                                   DataSnapshot dataSnapshot) {
                // Transaction completed
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
    }

    @Override
    public void onDataLoaded(Game game) {
        mCategoryText.setText("but");
        setGameInfo(game);
    }
}

When you app is put in the background and brought back, it can kill the fragment and recreated it. If you method setGameInfo is being called with nulls its because the method is being called between when the Fragment is recreated and when its onViewCreated (where you set up the view references) is called.

From your code, I have to guess there is something happening in the owning Activity that invokes the fragment's onDataLoaded method while the fragment is still be initialized.

Use your debugger to put a breakpoint at this method and in onViewCreated to see which is being called first and why.

I think the problem, or most part of it at least is where you are initializing the views.

As a rule of thumb, anything that needs to be created for the Fragment which is not a View should be declared in onCreate() , and anything with a findViewById() should go in onCreateView() , not onViewCreated() .

For example, I would move this part to onCreate():

    String dateFormat = SharedPrefManager.read(SharedPrefManager.KEY_PREF_DATE_FORMAT,      getString(R.string.default_date_format));
    mDateFormatterGMT = new SimpleDateFormat(dateFormat);
    mDateFormatterGMT.setTimeZone(TimeZone.getTimeZone("GMT"));
    mTodayTimeMillis = Calendar.getInstance().getTimeInMillis();
    mDatabaseHelper = DatabaseHelper.getDatabaseHelper(getActivity());
    mAlarmReceiver = new AlarmReceiver();

Also put here the creation of the adapter and the database helper:

mWebsitesAdapter = new WebsitesAdapter(getContext());

Other candidates for onCreate() are:

    String region = SharedPrefManager.read(SharedPrefManager.KEY_PREF_REGION, "north_america");
    mRegion = SharedPrefManager.getRegionToFilter(region);
    // Release social actions
    // The RELEASE DATA OBJECT
    mRelease = ((GamePageActivity)getActivity()).getRelease();
    mAuth = FirebaseAuth.getInstance();
    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            if (firebaseAuth.getCurrentUser() != null) {
                // When the user signs in in this page [refresh]
               initHypes(mRelease.getId());
            }
        }
    };

And most if not all you have in onViewCreated() to onCreateView()

I didn't run your code and it is quite long, so there may be exceptions to this in what you have done, but onViewCreated() should remian practically empty after the refactoring.

Note: I think the databasehelper is set twice, after moving it ot onCreate() you shouldn't need to repeat the initialization somewhere else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM