有一个基本的课程。为了简洁起见,我投得太多了。在这个活动开始的时候,所有的都能工作。
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:MessageQueue回调中的异常: handleReceiveCallback E/MessageQueue-JNI: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法的布尔handleReceiveCallback
我哪里错了?
发布于 2015-05-03 17:48:08
必须为您的recylcerview提供布局管理器,在后面添加以下内容
rv = (RecyclerView)findViewById(R.id.rv);
它也不会崩溃
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(layoutManager);
https://stackoverflow.com/questions/30016311
复制相似问题