首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >安卓。RecyclerView。错误类继承:...on为空对象引用

安卓。RecyclerView。错误类继承:...on为空对象引用
EN

Stack Overflow用户
提问于 2015-05-03 16:28:04
回答 1查看 2.1K关注 0票数 3

有一个基本的课程。为了简洁起见,我投得太多了。在这个活动开始的时候,所有的都能工作。

代码语言:javascript
运行
复制
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);
    }
}

扩展

代码语言:javascript
运行
复制
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

我哪里错了?

EN

回答 1

Stack Overflow用户

发布于 2015-05-03 17:48:08

必须为您的recylcerview提供布局管理器,在后面添加以下内容

rv = (RecyclerView)findViewById(R.id.rv);

它也不会崩溃

代码语言:javascript
运行
复制
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(layoutManager);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30016311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档