Reputation: 143
I currently have a fragment with a few buttons, a textview, and a listview.
Within the CustomAdapter I have an onClick method to remove the item from the listview when they click an image on the custom listview.
However, these are items with prices associated, and I want to update a textview that is sitting at the top of the fragment saying the "Total".
Here's some code.
(The on Click method within the ItemList Adapter)
holder.remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
_ItemObject itemToRemove = (_ItemObject) v.getTag();
remove(itemToRemove);
notifyDataSetChanged();
ItemList_Fragment itemlist = new ItemList_Fragment();
itemlist.resetTotal();
}
});
The function within my fragment named ItemList_Fragment
public void resetTotal() {
total = 0;
for (int i = 0; i < objects.size(); i++) {
total = total + (objects.get(i).getItemPrice() * objects.get(i).getItemQuantity());
}
quoteTotal.setText("$" + String.valueOf(String.format("%.2f", total + (total * 0.15))));
}
The error I get when I remove an item
05-09 10:15:54.172 5171-5171/quotenowv2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: quotenowv2, PID: 5171
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at quotenowv2.ItemList_Fragment.resetTotal(ItemList_Fragment.java:242)
at quotenowv2._ItemListAdapter$1.onClick(_ItemListAdapter.java:81)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I understand that it's saying my textview "doesn't exist" yet, but I'm unsure why as it was declared in my onViewCreated method at the beginning of the fragment.
If anyones looking for the complete answer:
final Context context = parent.getContext();
final FragmentManager fm = ((Activity) context).getFragmentManager();
final ItemList_Fragment itemList = (ItemList_Fragment)fm.findFragmentByTag("itemsListTag");
The above was set up in my getView method within the adapter, and the below was an example of how it was used in the onClick also within the adapters getView
holder.remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
_ItemObject itemToRemove = (_ItemObject) v.getTag();
remove(itemToRemove);
notifyDataSetChanged();
itemList.resetTotal();
}
});
Upvotes: 0
Views: 836
Reputation: 3654
That is because you are creating new instance of your fragment here:
ItemList_Fragment itemlist = new ItemList_Fragment();
Instead use getFragmentManager().findFragmentByTag(FRAGMENT_TAG)
or better use custom listener and listen for callbacks inside fragment.
Upvotes: 2