【问题标题】:Android. RecyclerView. Error class inheritance: ...on a null object reference安卓。回收站视图。错误类继承:...在空对象引用上
【发布时间】:2015-05-03 16:28:04
【问题描述】:

有一个基本类。为了简洁起见,我扔的太多了。在此活动开始时,一切正常。

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:尝试调用虚拟方法 '布尔值 android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' 在空对象引用上

我哪里错了?

【问题讨论】:

  • 有解决办法吗?
  • 你找到解决办法了吗?

标签: android android-recyclerview


【解决方案1】:

您必须向您的 recyclerview 提供布局管理器 在

之后添加以下内容

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

它不会崩溃

LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(layoutManager);

【讨论】:

    猜你喜欢
    • 2021-07-12
    • 2023-02-03
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多