Reputation: 348
There is a basic class. I threw too much for the sake of brevity. At the start of this activity all works.
public class MainActivity extends DrawerActivity {
protected Activity activity = this;
protected class GetDataFromMongoDb extends AsyncTask<String, Integer, ArrayList<CurrentNewsItem>> {
protected ArrayList<CurrentNewsItem> doInBackground(String... provider) {
//put data in array for adapter
}
protected void onPostExecute(final ArrayList<CurrentNewsItem> result) {
adapter = new RecyclerAdapter(result);
StaggeredGridLayoutManager llm = new StaggeredGridLayoutManager(UtilsScreen.getDisplayColumns((Activity) activity), StaggeredGridLayoutManager.VERTICAL);
rv.setLayoutManager(llm);
rv.setAdapter(adapter);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.activity_main, null, false);
mDrawer.addView(contentView, 0);
CurrentSection = ("news");
rv = (RecyclerView)findViewById(R.id.rv_main);
new GetDataFromMongoDb().execute(CurrentSection);
}
}
extended
public class News extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.activity_news, null, false);
mDrawer.addView(contentView, 0);
CurrentSection = getIntent().getExtras().getString("section");
rv = (RecyclerView)findViewById(R.id.rv);
new GetDataFromMongoDb().execute(CurrentSection);
}
}
When you go to News screen is blank, Clicking get the error:
E/InputEventReceiver﹕ Exception dispatching input event. E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback E/MessageQueue-JNI﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference
Where am I wrong?
Upvotes: 3
Views: 2106
Reputation: 5061
you must supply layout manager to your recylcerview add following just after
rv = (RecyclerView)findViewById(R.id.rv);
and it will not crash
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(layoutManager);
Upvotes: 2