简体   繁体   中英

onStart and Override Error in android studio

I wrote some code to get data from Firebase, but it shows some errors. I will explain the errors below and attach a picture.

在此处输入图片说明

First error when I put the mouse on it, message show: 'onStart()' in 'com.abdullrahman.eng.myapp.FriendsFragment' clashes with 'onStart()' in 'android.app.Fragment'; attempting to assign weaker access privileges ('protected'); was 'public'

Second error when I put the mouse on it, message show: Method does not override method from its superclass

Can anyone solve these problems and help me?

The code:

@Override
protected void onStart() {
    super.onStart();

    FirebaseRecyclerAdapter<Friends, FriendsViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Friends, FriendsViewHolder>(

            Friends.class,
            R.layout.users_single_layout,
            FriendsViewHolder.class,
            mUsersDatabase

    ) {
        @Override
        protected void populateViewHolder(FriendsViewHolder friendsViewHolder, Friends friends, int position) {

            friendsViewHolder.setDisplayName(friends.getName());
            friendsViewHolder.setUserStatus(friends.getStatus());
            friendsViewHolder.setUserImage(friends.getThumb_image(), mMainView.getContext());

            final String user_id = getRef(position).getKey();

            friendsViewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent profileIntent = new Intent(getActivity(), UserProfile.class);
                    profileIntent.putExtra("user_id", user_id);
                    startActivity(profileIntent);

                }
            });
        }
    };

    mUsersList.setAdapter(firebaseRecyclerAdapter);

}

Errors do say a lot about the problem you have.

First error is caused by protected access modifier on the onStart() method, while it should be public . It should be

@Override
publin void onStart() {
    super.onStart();
    // rest of the code
}

instead of

@Override
protected void onStart() {
    super.onStart();
    // rest of the code
}

You can find some more information about the error reason in docs available on Oracle site .

The second problem is related to the definition of FirebaseRecyclerAdapter. Looks like there is no method like

protected void populateViewHolder(FriendsViewHolder friendsViewHolder, Friends friends, int position).

I'd suggest to check docs/sources of this class to get info about how the overriden method should look like for the version of Firebase you are using in your project.

Also, as I can see you are using IntelliJ IDEA or some simmilar IDE, so you can use built-in feature to implement/override correct method .

Change your protected modifier to public and it'll go away.

In Java, the overriding method can have the same or weaker access compared to the parent method... Here the parent class declares the method as public so you have no other choice but to override it with a public access modifier as it's the weakest access possible.

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