有一个基础类,为了简洁起见,我添加了太多内容。在这个活动开始时一切都正常运作。
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);
}
}
扩展
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);
}
}
当您进入新闻屏幕时,屏幕是空白的,点击后会出现错误:
E/InputEventReceiver: 异常派发输入事件。E/MessageQueue-JNI: 消息队列中的异常回调:handleReceiveCallback E/MessageQueue-JNI: java.lang.NullPointerException: 尝试在null对象引用上调用'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()'方法
我哪里出错了?