android


Getting NPE when implementing interface callbacks between adapter and fragment


I am new in Android Development and I am using this code to implement callback between adapter and fragmnet.
BookAdapter
public class BookAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private OnButtonClickListener onButtonClickListener;
//List of books
private List<BookItems> mBookItems;
private final int VIEW_ITEM = 0;
private final int VIEW_PROG = 1;
private int lastPosition = -1;
public void OnButtonClickListener (OnButtonClickListener onButtonClickListener) {
this.onButtonClickListener = onButtonClickListener;
}
public interface OnButtonClickListener {
void onButtonClick();
}
public BookAdapter(List<BookItems> bookItems, Context context) {
super();
//Getting all books
this.mBookItems = bookItems;
this.mContext = context;
}
#Override
public int getItemViewType(int position) {
if (isPositionItem(position))
return VIEW_ITEM;
return VIEW_PROG;
}
private boolean isPositionItem(int position) {
return position != getItemCount()-1;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
if (viewType == VIEW_ITEM) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.book_summ, parent, false);
return new TextViewHolder(v);
} else if (viewType == VIEW_PROG){
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerfooter, parent, false);
return new ProgressViewHolder(v);
}
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof TextViewHolder) {
final BookItems bookList = mBookItems.get(position);
/...
} else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
((ProgressViewHolder) holder).loadButton.setText(R.string.reload);
}
}
#Override
public int getItemCount(){
//Return the number of items in the data set
return mBookItems.size();
}
public class TextViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
...
}
public class ProgressViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
Button loadButton;
ProgressBar progressBar;
public ProgressViewHolder(View footerView){
super(footerView);
...
}
#Override
public void onClick(View v) {
if (v.getId() == loadButton.getId()) {
onButtonClickListener.onButtonClick();
}
}
}
}
BookFragment
public class BookFragment extends Fragment implements BookAdapter.OnButtonClickListener{
BookAdapter mAdapter;
//Creating a list of books
private List<BookItems> mBookItemsList;
//Creating Views
ObservableRecyclerView recyclerView;
private String title;
private RecyclerView.Adapter adapter;
private ProgressDialog mProgressDialog;
SwipeRefreshLayout mSwipeRefreshLayout;
LinearLayoutManager mLayoutManager;
public BookFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new BookAdapter(mBookItemsList,getContext());
mAdapter.OnButtonClickListener(this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
title = getArguments().getString("title");
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_book, container, false);
//Initializing Views
...
return view;
}
...
#Override
public void onButtonClick() {
Log.d(TAG, "I recieved it Sir");
Toast.makeText(getActivity(), "blag blag", Toast.LENGTH_SHORT).show();
}
}
Each time I click on loadButton, the app crashes with nullpointer exception.
Do you have any idea why this is happening and how I can correct this?
Logcat
05-21 01:34:17.710 20717-20717/com.myapp.bookman E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.bookman, PID: 20717
java.lang.NullPointerException: Attempt to invoke interface method 'void com.myapp.bookman.BookAdapter$OnButtonClickListener.onButtonClick()' on a null object reference
at com.myapp.bookman.BookAdapter$ProgressViewHolder.onClick(BookAdapter.java:251)
at android.view.View.performClick(View.java:5076)
at android.view.View$PerformClick.run(View.java:20279)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
In the stacktrace line 251, is line onButtonClickListener.onButtonClick(); in BookAdapter
After Implementing TychoTheTaco answer
BookAdapter
public class BookAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
//List of books
private List<BookItems> mBookItems;
private final int VIEW_ITEM = 0;
private final int VIEW_PROG = 1;
private int lastPosition = -1;
public void OnButtonClickListener (OnButtonClickListener onButtonClickListener) {
this.onButtonClickListener = onButtonClickListener;
}
public interface OnButtonClickListener {
void onButtonClick();
}
public BookAdapter(List<BookItems> bookItems, Context context) {
super();
//Getting all books
this.mBookItems = bookItems;
this.mContext = context;
}
#Override
public int getItemViewType(int position) {
if (isPositionItem(position))
return VIEW_ITEM;
return VIEW_PROG;
}
private boolean isPositionItem(int position) {
return position != getItemCount()-1;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
if (viewType == VIEW_ITEM) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.book_summ, parent, false);
return new TextViewHolder(v);
} else if (viewType == VIEW_PROG){
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerfooter, parent, false);
return new ProgressViewHolder(v);
}
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof TextViewHolder) {
final BookItems bookList = mBookItems.get(position);
/...
} else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
((ProgressViewHolder) holder).loadButton.setText(R.string.reload);
}
}
#Override
public int getItemCount(){
//Return the number of items in the data set
return mBookItems.size();
}
public class TextViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
...
}
public class ProgressViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
Button loadButton;
ProgressBar progressBar;
public ProgressViewHolder(View footerView){
super(footerView);
...
}
#Override
public void onClick(View v) {
if (v.getId() == loadButton.getId()) {
onButtonClickListener.onButtonClick();
}
}
}
}
BookFragment
public class BookFragment extends Fragment implements BookAdapter.OnButtonClickListener{
BookAdapter mAdapter;
//Creating a list of books
private List<BookItems> mBookItemsList;
//Creating Views
ObservableRecyclerView recyclerView;
private String title;
private RecyclerView.Adapter adapter;
private ProgressDialog mProgressDialog;
SwipeRefreshLayout mSwipeRefreshLayout;
LinearLayoutManager mLayoutManager;
public BookFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new BookAdapter(mBookItemsList,getContext());
mAdapter.OnButtonClickListener(this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
title = getArguments().getString("title");
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_book, container, false);
//Initializing Views
...
return view;
}
...
OnButtonClickListener onButtonClickListener = new OnButtonClickListener() {
#Override
public void onButtonClick() {
Log.d(TAG, "I recieved it Sir");
Toast.makeText(getActivity(), "blag blag", Toast.LENGTH_SHORT).show();
}
}
}
The problem with this is that onButtonClickListener in line this.onButtonClickListener = onButtonClickListener; in BookAdapter, Android studio is saying "cannot resolve symbol onButtonClickListener"
And also, in BookFragment,in line public class BookFragment extends Fragment implements BookAdapter.OnButtonClickListener I nust either declare the class abstract or implement onButtoClick() in OnButtonClickListener.
You create your private OnButtonClickListener onButtonClickListener; but it is never assigned so when you call onButtonClickListener.onButtonClick(); it gives a NPE.
In BookAdapter, replace
private OnButtonClickListener onButtonClickListener;
with
OnButtonClickListener onButtonClickListener = new OnButtonClickListener(){
#Override
public void onButtonClick(){
//Do stuff
}
};

Related Links

View will get twice at onItemClick
downloading xml from ftp in android
Notification upon ended parsing of web response
How to transfer “data” between two device (android,iphone)?
write spinner selected value to database
Using AsyncTask to download an XML file
Screen resolution / Physical size
android - showDialog(); error
linux joining multicast group setsockopt returns errno 19 device not found
Foreign keys in android 2.2 and higher
HttpURLConnection, HttpClient and HttpPost request not working with POST request in Android
Android custom row layout in ListView with flexible height
Save image to sdcard from drawble resource on Android
Is it possible to combine an Accelerometer and Digital Compass to get a fine Gyroscope?
How to move a SurfaceView with transparent background?
maintaining view state android

Categories

HOME
design-patterns
eslint
object
mysql-workbench
serialization
alarm
access
google-cloud-functions
database-normalization
echo
vsm
crystal-reports-2013
mvc5
gluon-mobile
angularfire2
flume
pcl-crypto
vimdiff
reactive-programming
startup
spring-social
symmetricds
linear-algebra
android-7.0-nougat
imessage
sse
asp.net-core-webapi
database-connection
gulp-watch
pagespeed
highstock
beautifier
outliers
custom-controls
scrollview
mockjax
hibernate-validator
rselenium
android-sugarorm
android-maps-extensions
openwhisk
pdw-file-browser
jwplayer6
processwire
tex
recurrent-neural-network
urlsession
tasm
xv6
softmax
preloader
glm-math
optimizely
lightning
trojan
context-switch
custom-font
boo
check-mk
java-websocket
pybrain
qtplugin
flex4.6
burrows-wheeler-transform
installshield-2010
tooleap
respondcms
database-relations
spring-retry
rewrite
pypiserver
toolkit
dnssec
osc
zend-currency
nodelist
cosine-similarity
administration
entry-point
xml-dsig
mutual-authentication
android-framework
terminate
fpdi
getopt
model-driven-development
poker
composite-component
insert-id
mantle
http-status-code-410
fb.ui
frameworkelementfactory
boost-signals
thttpd
jsr286
listactivity
google-ajax-api
tournament
twitterizer
mtu
aspmenu
hbm2java
lalr
httpcookie

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App