RecyclerView中Adapter。<init>上的NullPointerException

时间:2019-07-18 07:17:06

标签: java android android-recyclerview nullpointerexception recycler-adapter

我经常从发布的应用程序中收到此错误。该应用程序在我初始化ArrayList的行中的适配器构造函数中崩溃。

该应用程序可以在我使用的模拟器和设备上完美运行,因此我不知道是什么原因造成的。

错误日志

  

Stacktrace

Caused by: java.lang.NullPointerException: 
  at java.util.ArrayList.<init> (ArrayList.java:191)
  **at com.nullbytesoftware.myapplication.Adapter.<init> (Adapter.java:24)
  at com.nullbytesoftware.myapplication.Emotes.CreateViews (Emotes.java:273)
  at com.nullbytesoftware.myapplication.Emotes.onCreate (Emotes.java:94)**
  at android.app.Activity.performCreate (Activity.java:7032)
  at android.app.Activity.performCreate (Activity.java:7023)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1236)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2821)

我在哪里创建我的回收站视图

private void CreateViews( ArrayList<Emote> arr) {
    //arr is a static ArrayList that i created & populated in MainActivity.
    recyclerView = findViewById(R.id.recyclerview);
    adapter = new Adapter(arr, this);

    recyclerView.setLayoutManager(new GridLayoutManager(this, 3));

    recyclerView.setAdapter(adapter);
}

我的适配器类:

public Adapter(ArrayList<Emote> arr, CommonMethods commonMethodsInterface) {
    this.emoteArray = new ArrayList<>(arr); //here is where it crashes.
    this.emoteArrayFull = new ArrayList<>(arr);

    this.commonMethodsInterface = commonMethodsInterface;
}

在我的MainActivity中,我这样创建ArrayList(它包含大约100个项目):

myArray.add(new Emote("name",R.raw.soundFile,R.drawable.icon,"type","rarity"));

0 个答案:

没有答案