0

I have a fragment, an activity, and an adapter in my UI. Here is the name of all these program:

enter image description here

The page right now is in 1 fragment which is homePageFragment. The number[1-5] is the fragment tab store in MainActivity. The MainAdapter is the list of post comes with user icons(which is in blue color). The homePageFragment is to store 1 fragment. The OtherProfileFragment is the Fragment I want to transfer to if the users click the icon, It will transfer from 1 to [3]. Here is the code I've done on the MainAdapter:

Picasso.get().load(pPicUrl).into(holder.profileView);
        holder.profileView.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("debug_routing", pPicUrl + "<-url");
                //transfer based on user id
                Query q = FirebaseFirestore.getInstance().collection("users").whereEqualTo("username", userName);
                q.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if(task.isSuccessful()){
                            userInfo.clear();
                            for(QueryDocumentSnapshot document : task.getResult()){
                                String uid = document.getString("user_id");
                                Log.d("debug_routing", "found uid ->" + uid);
                                find_uid = uid;
                                if(uid == null){
                                    Toast.makeText(getApplicationContext(), "No related user found", Toast.LENGTH_SHORT).show();
                                }
                                Bundle bundle = new Bundle();
                                Log.d("debug_routng", find_uid + " " + userName + " " + fullName + " " + pPicUrl);
                                userInfo.add(find_uid);
                                userInfo.add(userName);
                                userInfo.add(fullName);
                                userInfo.add(pPicUrl);

                                otherProfileFragment = new OtherProfileFragment();

                                bundle.putStringArrayList("user_info", userInfo);
                                otherProfileFragment.setArguments(bundle);
                                FragmentManager manager = ((AppCompatActivity)context).getSupportFragmentManager();
                                manager.beginTransaction().replace(R.id.feed_container, otherProfileFragment).commit();

                                mWallButton.setBackgroundColor(Color.parseColor("#ECEFF1")); //Error Occur Here
                                homeButton.setBackgroundColor(Color.WHITE); //Error also
                            }
                        }else {
                            Log.d("debug_routing", "cannot found");

                        }
                    }
                });
                Toast.makeText(v.getContext(), "The icon is clicked", Toast.LENGTH_LONG).show();
            }
        });

Everything works until the mWallButton line, which is declared as a ImageButton [3] in MainActivity. The mWallButton should turn grey since I redirect from 1 to [3]. 1 ImageButton will turn white and [3] will turn grey.
When I click the blue icon, it will show this error below:

Attempt to invoke virtual method 'void android.widget.ImageButton.setBackgroundColor(int)' on a null object reference

This is how I declare mWallButton in MainActivity.

mWallButton = findViewById(R.id.navigation_wall);

My question is, how can I declare mWallButton(MainActiivty) in adapter? I've search many tutorial but out of luck. Any help or comment would appropriate. Thanks

Noizy Z
  • 81
  • 11

1 Answers1

0

You can not initialize the button in Adapter. what you can do is make a public function to change the button background color in your Activity and access it from the Adapter when needed. If you need resource to do it please visit this Call Activity method from adapter

Jyotish Biswas
  • 674
  • 5
  • 11