Empty RecyclerView with data obtained from parse.com
I’m currently working on implementing data fetching
from a class
I created on parse.com
. I want to retrieve an object named “flyerDataFetch” and all its elements to display in Recyclerview
.
When I run and compile the program, I get the error:
error:java.lang.NullPointerException at android.view.LayoutInflater.from(LayoutInflater.java:211)
Error: RecyclerView﹕ No adapter attached; skipping layout.
Could you please examine my code and help determine what went wrong while trying to populate my RecyclerView
?
Main Activity
:
public class HomeScreen extends AppCompatActivity {
private RecyclerView mRecyclerDrawer;
private customAdapter mAdapter;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
setupDrawer();
final List<Information> data = new ArrayList<>();
Parse.initialize(this, "xxxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxx");
ParseQuery<ParseObject> query = ParseQuery.getQuery("flyerDataFetch");
query.getInBackground("xWMyZ4YEGZ", new GetCallback<ParseObject>() {
@Override
public void done(ParseObject parseObject, com.parse.ParseException e) {
if (e == null) {
Information information = new Information();
information.partyName = parseObject.getString("partyName");
information.partyPromoterName = parseObject.getString("partyCost");
information.partyCost = parseObject.getString("partyName");
information.flyerPic = parseObject.getParseFile("partyFlyerImage");
information.partyPromoterPic = parseObject.getParseFile("partyPromoterImage");
data.add(information);
} else {
something went wrong
}
mRecyclerDrawer = (RecyclerView) findViewById(R.id.drawerList);
mRecyclerDrawer.setLayoutManager(new LinearLayoutManager(context));
mAdapter = new customAdapter(context, data);
mRecyclerDrawer.setAdapter(mAdapter);
}
});
}
My app
:
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
ParseCrashReporting.enable(this);
Parse.enableLocalDatastore(this);
Parse.initialize(this, "xxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxx");
}
Custom adapter
:
package com.example.jbobo_000.prac;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.parse.ParseImageView;
import java.util.Collections;
import java.util.List;
public class customAdapter extends RecyclerView.Adapter<customAdapter.MyViewHolder> {
List<Information> data = Collections.emptyList();
private LayoutInflater inflater;
private Context context;
public customAdapter (Context context,List<Information>data){
this.context=context;
inflater=LayoutInflater.from(context);
this.data= data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view= inflater.inflate(R.layout.custom_row, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder,int position){
Information current= data.get(position);
holder.promoterNameText.setText(current.partyPromoterName);
holder.costText.setText(current.partyCost);
holder.partyNameText.setText(current.partyName);
holder.flyerImage.setParseFile(current.flyerPic);
holder.promoterImage.setParseFile(current.partyPromoterPic);
}
@Override
public int getItemCount(){
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView promoterNameText;
TextView costText;
TextView partyNameText;
ParseImageView flyerImage;
ParseImageView promoterImage;
public MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
promoterImage = (ParseImageView) itemView.findViewById(R.id.promoterPicImage);
flyerImage = (ParseImageView) itemView.findViewById(R.id.flyertImage);
costText = (TextView) itemView.findViewById(R.id.costText);
promoterNameText = (TextView) itemView.findViewById(R.id.promoterNameText);
partyNameText = (TextView) itemView.findViewById(R.id.partyNameText);
}
@Override
public void onClick(View v) {
context.startActivity(new Intent(context, partyInformation.class));
}
}
}
Info
:
package com.example.jbobo_000.prac;
import com.parse.ParseFile;
import com.parse.ParseImageView;
public class Information {
public ParseFile flyerPic;
public ParseFile partyPromoterPic;
public String partyCost;
public String partyName;
public String partyPromoterName;
}
Logcat
:
06-12 01:52:37.511 5432-5432/com.example.jbobo_000.prac E/RecyclerView﹕ No adapter attached; skipping layout
06-12 01:52:37.766 5432-5432/com.example.jbobo_000.prac D/AndroidRuntime﹕ Shutting down VM
06-12 01:52:37.766 5432-5432/com.example.jbobo_000.prac E/CrashReporting﹕ ParseCrashReporting caught a NullPointerException exception for com.example.jbobo_000.prac. Building report.
06-12 01:52:37.768 5432-5432/com.example.jbobo_000.prac E/CrashReporting﹕ Handling exception for crash
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.view.LayoutInflater.from(LayoutInflater.java:219)
at com.example.jbobo_000.prac.customAdapter.<init>(customAdapter.java:25)
at com.example.jbobo_000.prac. HomeScreen$1.done(HomeScreen.java:57)
at com.example.jbobo_000.prac. HomeScreen$1.done(HomeScreen.java:38)
at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:107)
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:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-12 01:52:37.768 5432-5432/com.example.jbobo_000.prac D/CrashReporting﹕ Generating report file for crash
06-12 01:52:37.843 5432-5432/com.example.jbobo_000.prac D/CrashReporting﹕ Retrieving logcat output...
06-12 01:52:37.871 5432-5432/com.example.jbobo_000.prac D/CrashReporting﹕ Retrieving logcat output...
06-12 01:52:37.889 5432-5432/com.example.jbobo_000.prac D/CrashReporting﹕ Retrieving logcat output...
06-12 01:52:37.965 5432-5432/com.example.jbobo_000.prac W/Settings﹕ Setting stay_on_always has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
06-12 01:52:38.164 5432-5432/com.example.jbobo_000.prac V/CrashReporting﹕ About to start ReportSenderWorker from #handleException
06-12 01:52:38.172 5432-5603/com.example.jbobo_000.prac D/CrashReporting﹕ #checkAndSendReports - start
06-12 01:52:38.172 5432-5603/com.example.jbobo_000.prac D/CrashReporting﹕ Looking for error files in /data/data/com.example.jbobo_000.prac/files/com.parse/cr/reports
06-12 01:52:38.175 5432-5603/com.example.jbobo_000.prac D/CrashReporting﹕ Loading file 1434088357768-NullPointerException-1.stacktrace
06-12 01:52:38.185 5432-5603/com.example.jbobo_000.prac I/CrashReporting﹕ Sending file 1434088357768-NullPointerException-1.stacktrace
06-12 01:52:38.186 5432-5603/com.example.jbobo_000.prac D/CrashReporting﹕ Sending crash report to Parse...
06-12 01:52:38.193 5432-5603/com.example.jbobo_000.prac D/CrashReporting﹕ #checkAndSendReports - finish
06-12 01:52:38.265 5432-5432/com.example.jbobo_000.prac E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.jbobo_000.prac, PID: 5432
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.view.LayoutInflater.from(LayoutInflater.java:219)
at com.example.jbobo_000.prac.customAdapter.<init>(customAdapter.java:25)
at com.example.jbobo_000.prac. HomeScreen$1.done(HomeScreen.java:57)
at com.example.jbobo_000.prac. HomeScreen$1.done(HomeScreen.java:38)
at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:107)
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:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-12 01:52:38.291 5432-5442/com.example.jbobo_000.prac W/art﹕ Suspending all threads took: 5.494ms
Solution
First you don’t get all the data for the table (object) flyerDataFetch, you just get a specific column of data, id “xWMyZ4YEGZ” your query should be
ParseQuery<ParseObject> query = ParseQuery.getQuery("flyerDataFetch");
query.findInBackgroud(new FindCallBack<TableClass>(){
@Override
public void done(ParseObject List<TableName> list, com.parse.ParseException e) {
if (e == null) {
GENERATE LOOP HERE AND GET ALL DATA OF LIST INTO YOUR LOCAL LIST WHICH YOU ARE PASSING TO ADAPTER OF RECYCLER VIEW
I am telling you to add loop so you can learn..
Information information = new Information();
information.partyName = parseObject.getString("partyName");
information.partyPromoterName = parseObject.getString("partyCost");
information.partyCost = parseObject.getString("partyName");
information.flyerPic = parseObject.getParseFile("partyFlyerImage");
information.partyPromoterPic = parseObject.getParseFile("partyPromoterImage");
data.add(information);
} else {
something went wrong
}
This will get all the data from the table and set it in the list that you passed to the adapter. This could be the reason for your NullPointerException!
Another thing is that the way you get the image is wrong
You actually need to execute the query and get the bytes[] array, which you can then set in ParseImageView
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options);
myViewHolder.myImageView.setBitmapImage(bmp);
Also, you don’t need context in your customAdapter: just write this
private LayoutInflater inflater;
public customAdapter (Context context,List<Information>data){
inflater=LayoutInflater.from(context);
this.data= data;
}
Also in your MainActivity class: Change this
mRecyclerDrawer.setLayoutManager(new LinearLayoutManager(context));
mAdapter = new customAdapter(context, data);
to
mRecyclerDrawer.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new customAdapter(getApplicationContext(), data);
.. Declare your following data
final List<Information> data = new ArrayList<>();
In the MainActivity before the onCreate() method:
Hope this helps!