首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >垂直LinearLayoutManager崩溃应用

垂直LinearLayoutManager崩溃应用
EN

Stack Overflow用户
提问于 2015-12-15 05:01:16
回答 1查看 581关注 0票数 1

因此,我在我的安卓应用程序中的片段中使用了两个RecyclerViews。其中一个水平滚动,另一个垂直滚动。然而,由于某种奇怪的原因,垂直的那个总是崩溃,并出现以下错误:

代码语言:javascript
复制
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollHorizontally()' on a null object reference

下面是我如何设置RecyclerViews的:

代码语言:javascript
复制
@InjectView(R.id.nearby_recycler)
RecyclerView nearbyRecycler;
RecyclerView.LayoutManager nearbyLayoutManager;

@InjectView(R.id.buddies_recycler)
RecyclerView buddiesRecycler;
RecyclerView.LayoutManager buddiesLayoutManager;

public static HomeFragment newInstance(){
    return new HomeFragment();
}

public HomeFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View root = inflater.inflate(R.layout.fragment_home, container, false);
    ButterKnife.inject(this, root);

    nearbyLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
    nearbyRecycler.setLayoutManager(nearbyLayoutManager);
    nearbyRecycler.setAdapter(buddiesAdapter);

    buddiesLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    buddiesRecycler.setLayoutManager(buddiesLayoutManager);
    buddiesRecycler.setAdapter(buddiesAdapter);

    return root;
}

fragment_home.xml中的RecyclerViews

代码语言:javascript
复制
<android.support.v7.widget.RecyclerView
    android:id="@+id/nearby_recycler"
    android:layout_width="match_parent"
    android:layout_height="@dimen/icon_large"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/buddies_recycler"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

只要我将方向切换到水平方向,RecyclerView就会在我滚动它的时候崩溃。有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2015-12-15 08:05:16

我想通了!所以我完全不确定为什么这是一个问题,但问题的根源是我的片段在视图分页程序中。我暂时将其从视图分页程序中删除,并通过片段事务添加了片段。

我之前添加片段的方法是这样的。

MainActivity.java

代码语言:javascript
复制
HomeFragment homeFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.inject(this);

    homeFragment = HomeFragment.newInstance();
    mFragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            return homeFragment;
        }

        @Override
        public int getCount() {
            return 1;
        }
    };
}

activity_main.xml

代码语言:javascript
复制
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    tools:context=".ui.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_below="@id/toolbar1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

我将其更改为:

MainActivity.java

代码语言:javascript
复制
HomeFragment homeFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.inject(this);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, homeFragment)
                .commit();
    }
}

activity_main.xml

代码语言:javascript
复制
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    tools:context=".ui.MainActivity">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34276517

复制
相关文章

相似问题

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