简体   繁体   中英

Calling methods in Activity from RecyclerView adapter class

I have an activity with a recyclerview and a button. In my recyclerview adapter in my onBindViewHolder, I set an onClickListener to the layout of the list item. When this is clicked, I want to pass the Contact (list item type) that was selected to my activity and then into a list so that it can be sent as a request to my server.

When the item is clicked, I want the colour to change to a highlighted blue colour, and for the button at the bottom of the screen to say "Continue - x contacts" (x = however many contacts are selected.) I had this working until I added the button to the activity.

The problem I'm having, is that the button isn't associating with the button ID, and it is crashing out after an item click because setText() cannot be called on a null object reference.

Error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference

I don't understand why the button is null, unless it is the way that I'm calling my activity object from the adapter.

Appreciate any help or advice!

RecyclerViewAdapter: (I have the other methods and ViewHolder class in the adapter and they are working fine. This is the only method that is effecting the result afaik)

private CreateGroupAccountStage2 createGroupAccountStage2 = new CreateGroupAccountStage2();
public List<Contacts> contactsList;
public List<Contacts> selectedContacts = new ArrayList<>();

@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) {
final Contacts contacts = contactsList.get(position);
viewHolder.contactName.setText(contacts.getContactName());
viewHolder.contactItem.setOnClickListener(new View.OnClickListener() { //Set on click listener to item layout
  @Override
  public void onClick(View view) {
    if (!contacts.getIsPressedValue()) {
      contacts.setPressedTrue(); // Method that changes boolean value stored in the Contact object
      viewHolder.contactItem.setBackgroundResource(R.color.createGroupAccountContactItemPressed);
      selectedContacts.add(contacts);
    } else {
      viewHolder.contactItem.setBackgroundResource(R.color.whiteText);
      contacts.setPressedFalse(); // Method that changes boolean value stored in the Contact object
      selectedContacts.remove(contacts);
    }
    createGroupAccountStage2.updateSelectedContacts(contactsList);
  }
});
}

Activity:

Button stage2Continue;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_create_group_stage2);

  stage2Continue = (Button) findViewById(R.id.createGroupAccountStage2ContinueBTN);
  setUpActionBar();
  getSupportActionBar().setDisplayHomeAsUpEnabled(true);

  ArrayList<Contacts> fullContactsList = new ArrayList<>();
  fullContactsList.add(new Contacts(R.drawable.human_photo, "Human One", "humanone@gmail.com"));
  fullContactsList.add(new Contacts(R.drawable.human_photo, "Human Two", "humantwo@gmail.com"));

  setUpContactsRecyclerView(fullContactsList); //Sets up recycler view (works fine)
}

public void updateSelectedContacts(List<Contacts> contacts) {
  int listSize = contacts.size();
  updateContinueButton(listSize);
}

public void updateContinueButton(int selectedContactsListSize) {
  String listSize = Integer.toString(selectedContactsListSize);
  String buttonText = "Continue - " + listSize + " contacts";
  if(selectedContactsListSize > 0) {
    stage2Continue.setText(buttonText);
  } else {
    stage2Continue.setVisibility(View.INVISIBLE);
  }
}

Layout XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="grouppay.dylankilbride.com.activities.CreateGroupAccountStage2">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ToolbarTheme">

<android.support.v7.widget.Toolbar
    android:id="@+id/createAccountStage2Toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/whiteText"
    app:popupTheme="@style/ToolbarTheme" />

</android.support.design.widget.AppBarLayout>

<Button
    android:id="@+id/createGroupAccountStage2ContinueBTN"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_margin="40dp"
    android:layout_alignParentBottom="true"
    android:text="@string/generic_continue"
    android:textSize="15sp"
    android:visibility="invisible"
    android:textColor="@color/whiteText"
    android:background="@drawable/generic_rounded_continue_button_bg"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/createGroupAccountStage2ContactsRV"
    android:layout_width="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>

Contact List Item Layout XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center_vertical"
android:padding="10dp"
android:id="@+id/createGroupAccountStage2ContactLL">

<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/createGroupAccountStage2ContactImgTV"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="@drawable/human_photo"
    app:civ_border_width="1dp"
    app:civ_border_color="@color/profileImageBorder"/>

<TextView
    android:id="@+id/createGroupAccountStage2ContactNameTV"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:textSize="20sp"
    android:fontFamily="@font/cairo"
    tools:text="Human Example"
    />

</LinearLayout> 

It seems you need to implement onCreateViewHolder() in your RecyclerViewAdapter so that the ViewHolder is inflated with the layout including your createGroupAccountStage2ContinueBTN button

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = mInflater.inflate(R.layout.your_item_layout, parent, false);
    return new ViewHolder(itemView);
}

检查您的导入,如果在您的代码中导入了正确的contactName视图,则您有可能在任何其他xml文件中的另一个视图中使用相同的ID,并且由于导入错误,该视图将为null,因为该视图不是充气。

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