Java 在android 5.0上尝试使用RecyclerView时应用程序崩溃

Java 在android 5.0上尝试使用RecyclerView时应用程序崩溃,java,android,android-layout,android-recyclerview,android-5.0-lollipop,Java,Android,Android Layout,Android Recyclerview,Android 5.0 Lollipop,我试图搞乱新的RecyclerView,每当我尝试运行它时,我的应用程序立即崩溃。它让我对尝试从android.support.v7.widget.RecyclerView访问方法产生了NullPointerException。我看过其他帖子,发现大多数人都没有编译com.android.support:recyclerview-v7:+”,但我试过了,结果毫无帮助。现在还不确定该怎么办,如果有任何帮助,我们将不胜感激。这里是错误日志:(我会发布一张照片,但我还没有10个代表) 此问题通常发生在

我试图搞乱新的RecyclerView,每当我尝试运行它时,我的应用程序立即崩溃。它让我对尝试从
android.support.v7.widget.RecyclerView
访问方法产生了NullPointerException。我看过其他帖子,发现大多数人都没有编译com.android.support:recyclerview-v7:+”,但我试过了,结果毫无帮助。现在还不确定该怎么办,如果有任何帮助,我们将不胜感激。这里是错误日志:(我会发布一张照片,但我还没有10个代表)


此问题通常发生在没有为提供的情况下。你可以这样做:

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

就我而言,这与“最终”无关,而是与@NemanjaKovačević对@aga answer的评论中提到的问题有关。 我在数据加载时设置了一个layoutManager,这就是导致相同崩溃的原因。 将layoutManager设置移动到我的片段的onCreateView后,问题得到了解决

大概是这样的:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
{
...
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler);

mLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);

即使正确设置了
RecyclerView.LayoutManager
,我还是经历了这次崩溃。我必须将
RecyclerView
初始化代码移到
onViewCreated(…)
回调中才能解决此问题

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_listing, container, false);
    rootView.setTag(TAG);
    return inflater.inflate(R.layout.fragment_listing, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    mLayoutManager = new LinearLayoutManager(getActivity());
    mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

    mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new ListingAdapter(mListing);
    mRecyclerView.setAdapter(mAdapter);
}

由于LinearLayoutManager在默认情况下是垂直的,因此更简单的方法是:

recyclerView.setLayoutManager(新的LinearLayoutManager(上下文))

如果要更改方向,可以使用此构造函数:

public LinearLayoutManager(Context context, int orientation, boolean reverseLayout);

对我来说,我也遇到了同样的问题,问题是xml中有一个未使用的RecyclerView,视图已不存在,但我没有将其绑定到活动中的任何适配器,因此出现了问题。当我在xml中删除这些未使用的回收器视图时,问题就迎刃而解了

i、 e-我删除了这个视图,因为代码中没有调用它,或者已经设置了任何适配器


您需要在
RecyclerView#onCreate()方法中使用
setLayoutManager
。在将
recyclerView
添加到视图之前,必须设置
LayoutManager

 private RecyclerView menuAsList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    menuAsList = (RecyclerView) findViewById(R.id.recyclerView_mainMenu);
    menuAsList.setLayoutManager(new LinearLayoutManager(Home.this));

}

我得到这个问题是因为错误地引用了
RecyclerView
id

recyclerView = (RecyclerView) findViewById(R.id.rv_followers_list);


与您的回收器视图ID一起检查,指向实际的回收器视图解决了我的问题

我认为您的
适配器中的问题
。 确保已在
onCreateViewHolder()
中返回
ViewHolder
。 如下图所示:

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v;
    v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_leaderboard, parent, false);
    ViewHolder view_holder = new ViewHolder(v);
    return view_holder;
}

我的问题出在XML lyout中,但我将android:animateLayoutChanges设置为true,并且在Java代码中调用了RecyclerView适配器上的notifyDataSetChanged()


所以,我刚刚从我的布局中删除了android:animateLayoutChanges,这重新解决了我的问题。

我在使用Butterknife库时遇到了这个问题。
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
我有:

但正确的版本是:

View rootView = inflater.inflate
                (R.layout.fragment_recipe_detail_view, container, false);
ButterKnife.bind(this, rootView);

如果我知道你在哪里,我会这么做。 onView(带id(android.R.id.list)).perform(recycleServiceActions.scrollToPosition(3));
android.R.id.list将其更改为您的列表id,位置将是您在阵列中的位置。

对我来说,问题在于我的activity_main.xml(21)中的recycleView没有id

activity_main.xml(21)


activity_main.xml

<android.support.v7.widget.RecyclerView

    android:id="@+id/transac_recycler_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button3"
    />


当我在Gradle中的activity_main.xml(21)recycleView中添加一个android:id=“@+id/transac_recycler_view”时,它起到了作用。实现此库:

implementation 'com.android.support:appcompat-v7:SDK_VER'
implementation 'com.android.support:design:SDK_VER'
implementation 'com.android.support:support-v4:SDK_VER'
implementation 'com.android.support:support-v13:SDK_VER'
implementation 'com.android.support:recyclerview-v7:SDK_VER'
XML中这样提及:

<android.support.v7.widget.RecyclerView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/recyclerView"/>
活动中我们使用
回收视图
如下:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       View rootView = inflater.inflate(R.layout.YOUR_LAYOUT_NAME, container, false);

       RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        APICallMethod();
        YOURADAPTER = new YOURADAPTER(getActivity(), YOUR_LIST); // you can use that adapter after for loop in response(APICALLMETHOD)
        recyclerView.setAdapter(YOURADAPTER);
        return rootView;
    }
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.YOUR_LAYOUT_NAME);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        APICallMethod();
        YOURADAPTER = new YOURADAPTER(ACTIVITY_NAME.this, YOUR_LIST); // you can use that adapter after for loop in response(APICALLMETHOD)
        recyclerView.setAdapter(YOURADAPTER);
   }
        RecyclerView recyclerView = findViewById(R.id.rvNumbers);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3)); // 3 is number of columns.
        adapter = new MyRecyclerViewAdapter(this, data);
        recyclerView.setAdapter(adapter);
如果需要水平
回收视图
执行以下操作:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(layoutManager);
        APICallMethod();
        YOURADAPTER = new YOURADAPTER(ACTIVITY_NAME.this, YOUR_LIST); // you can use that adapter after for loop in response(APICALLMETHOD)
        recyclerView.setAdapter(YOURADAPTER);
如果您想使用GridLayoutManager使用
RecyclerView
可以这样使用:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       View rootView = inflater.inflate(R.layout.YOUR_LAYOUT_NAME, container, false);

       RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        APICallMethod();
        YOURADAPTER = new YOURADAPTER(getActivity(), YOUR_LIST); // you can use that adapter after for loop in response(APICALLMETHOD)
        recyclerView.setAdapter(YOURADAPTER);
        return rootView;
    }
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.YOUR_LAYOUT_NAME);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        APICallMethod();
        YOURADAPTER = new YOURADAPTER(ACTIVITY_NAME.this, YOUR_LIST); // you can use that adapter after for loop in response(APICALLMETHOD)
        recyclerView.setAdapter(YOURADAPTER);
   }
        RecyclerView recyclerView = findViewById(R.id.rvNumbers);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3)); // 3 is number of columns.
        adapter = new MyRecyclerViewAdapter(this, data);
        recyclerView.setAdapter(adapter);
我使用的是依赖注入-ButterKnife,出现这个问题是因为我没有绑定视图

在onCreate()中,我插入了这一行:

ButterKnife.bind(this);
带有依赖项注入的My RecyclerView声明:

@BindView(R.id.recyclerview)
RecyclerView recyclerView;
使用片段

在onCreateView()中编写代码

RecyclerView recyVideo=view.findViewById(R.id.recyVideoFag)

使用活动

在onCreate()中编写代码
RecyclerView recyVideo=findViewById(R.id.recyVideoFag)

在我的例子中,我只添加了butterknife库,而忘记添加annotationProcessor。通过在build.gradle(应用程序模块)中添加以下行,解决了我的问题

    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'

Recyclerview recylerview=findbyid(R.id.Recyclerview)
对于那些正在使用:实现“androidx.recyclerview:recyclerview:1.1.0-beta05”的用户,您可能错过了它

确保您在xml布局中使用了它

在API 27以下运行我的应用程序时,我也遇到了崩溃

最初,我在recyclerview中创建了一个样式来更改textview的字体大小,这就是为什么我创建了下面的代码:(我可以在根据屏幕大小更改字体大小时调用@dimen)

在我的主题中添加了这个

经过调查,我发现这是导致我出错的原因,在删除样式后,我不再经历崩溃


我尝试了在stack overflow中找到的所有可能的解决方案,几乎放弃了,幸好出于好奇,我想为什么不删除样式,可能是这导致了崩溃,然后砰的一声,崩溃消失了

在GenyMotion上测试应用程序时,我遇到了类似的问题。在GenyMoon中重新启动设备后,问题得到解决

我面临同样的问题,因为我将RecyclerView放在另一个布局中,而不是放在activity_main.xml上

通过搜索一些网站,我得到了一些东西,并想在这里分享

  • 添加Id以在Activity_main.xml中包含标记

    <android.support.v7.widget.RecyclerView
    
        android:id="@+id/transac_recycler_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button3"
        />
    

    我在使用Butterknife时遇到了这个问题,我使用的是fragment

    对于片段,它应该是
    ButterKnife.bind(这个,视图)


    对于活动
    ButterKnife.bind(此)

    我不认为它需要是最终的,所以我不认为这是最重要的