简体   繁体   English

如何修复回收器上的 NullPointerException 错误?

[英]How can i fix NullPointerException error on my recycler?

i'm trying to create RecycleView in my activity, but the app crashes at launch and the log says我正在尝试在我的活动中创建 RecycleView,但应用程序在启动时崩溃,日志显示

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
                                                   at com.punk.home.snoopwatches.MainActivity.onCreate(MainActivity.java:55)

I can't fix this all day, so i hoped you can My classes: MainActivity.class:我不能整天解决这个问题,所以我希望你可以我的课程:MainActivity.class:

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imagesIdList=new ArrayList<>();
    imagesIdList.add(R.drawable.palm);
    imagesIdList.add(R.drawable.aliens);
    imagesIdList.add(R.drawable.palm);
    imagesIdList.add(R.drawable.aliens);
    imagesIdList.add(R.drawable.palm);
    imagesIdList.add(R.drawable.aliens);
    imagesIdList.add(R.drawable.palm);
    imagesIdList.add(R.drawable.aliens);

    final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    layoutManager.generateDefaultLayoutParams();
    recyclerView=(RecyclerView) findViewById(R.id.my_recycler_view);
    recyclerView.setLayoutManager(layoutManager);
    //        recyclerView.setHasFixedSize(true);
    adapter=new ImageAdapter(MainActivity.this, imagesIdList);
    recyclerView.setAdapter(adapter);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(this);

}
RecyclerView recyclerView;
ArrayList<Integer> removedItems;
ArrayList<Integer> imagesIdList;
ImageAdapter adapter;

ImageAdapter.class图像适配器类

  ImageView imageView;

    public ImageViewHolder(View itemView) {
        super(itemView);
        imageView = (ImageView) itemView.findViewById(R.id.image);
    }
}

List<Integer> imageId;

ImageAdapter(Context context, List<Integer> imageId) {
    this.imageId = imageId;
    this.context = context;
}

Context context;

@Override
public ImageViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_image_activity, viewGroup, false);
    ImageViewHolder ivh = new ImageViewHolder(view);
    return ivh;
}

@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
    holder.imageView.setImageResource(imageId.get(position));
}


@Override
public int getItemCount() {
    return imageId.size();
}

and contnent_main.xml和 contnent_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.punk.home.snoopwatches.MainActivity"
    tools:showIn="@layout/activity_main">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

item_image_activity.xml item_image_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    tools:showIn="@layout/content_main"
    android:id="@+id/item_layout">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/image"/>

</LinearLayout>

MainActivity.xml主活动.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">


</android.support.design.widget.AppBarLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/add" />

RecyclerView setLayoutManager expects RecyclerView.LayoutManager type as input so change RecyclerView setLayoutManager 期望 RecyclerView.LayoutManager 类型作为输入,因此更改

final LinearLayoutManager layoutManager = new LinearLayoutManager(this);

with

final RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);

and remove并删除

layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.generateDefaultLayoutParams();

take a look at developer page for proper usagehttp://developer.android.com/training/material/lists-cards.html看看开发者页面是否正确使用http://developer.android.com/training/material/lists-cards.html

You need to put an <include> section for contnent_main.xml into activity_main.xml if you want to use findViewById and not have it return null.如果要使用findViewById并且不让它返回 null,则需要将contnent_main.xml<include>部分放入activity_main.xml

Something like <include layout="@layout/contnent_main"/> beneath the AppBarLayout.类似于<include layout="@layout/contnent_main"/>下的<include layout="@layout/contnent_main"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不知道如何修复错误NullPointerException - I can't figure out how to fix error NullPointerException 是什么引起“ NullPointerException”错误,我该如何解决? - What causes a “NullPointerException” error here and how can I fix it? 我的“回收者”视图未显示其拥有的所有物品,我该如何解决? - My recycler view doesn't show all items that it has, how can I fix it? NullPointerException错误:如何解决此问题? - NullPointerException Error: How do I fix this one? 如何修复我的 java 堆栈<t>我得到 nullpointerexception 的代码?</t> - How can I fix my java Stack<T> code where I got nullpointerexception? “ java.lang.NullPointerException”错误(错误详细信息,在本文中)我该如何解决? - “java.lang.NullPointerException” Error(Error details, there in the article) How can i fix it? 如何在我的代码中修复NullPointerException - How to fix the NullPointerException in my code 如何修复java.lang.NullPointerException(android)? - How do I fix my java.lang.NullPointerException (android)? nullpointerexception 错误我现在知道如何修复 - nullpointerexception error I do now know how to fix 如何在给定代码中修复java.lang.NullPointerException? - How can I fix the java.lang.NullPointerException in the given code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM