猿问

如何修复 recycleview 项目中的“尝试调用虚拟方法”错误?

我编写了一个代码,其目标是将数组列表项设置为回收视图。但是当我运行该项目时,应用程序将崩溃。


d.


我检查依赖关系。他们没事。我检查了代码,我认为它们没有任何问题。


//主活动类


public class MainActivity extends AppCompatActivity {


RecyclerView rcl_cities;

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    initViews();

    Log.e("ApplicationSays:","LayoutManager set");

}


private void initViews(){


    rcl_cities = findViewById(R.id.rcl_cities);


    rcl_cities.setLayoutManager(new LinearLayoutManager(this,RecyclerView.VERTICAL,false));


    CityAdapter cityAdapter = new CityAdapter(this,getCities());

    rcl_cities.setAdapter(cityAdapter);


}


private ArrayList<City> getCities()

{


    ArrayList<City> cities = new ArrayList<>();


    cities.add(new City(1, "mashhad"));

    cities.add(new City(2, "Tehran"));

    cities.add(new City(3, "Gorgan"));

    cities.add(new City(4, "Shiraz"));

    cities.add(new City(5, "Rasht"));


    return cities;

}

}


一只名叫tom的猫
浏览 113回答 1
1回答

哔哔one

首先,将您的更改view为xmlView<View&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="3dp"&nbsp; &nbsp; android:background="@android:color/holo_red_dark" />其次,你没有按照你的onBindViewHolder方法做正确的事情City city = cities.get(position);在尝试向 City 传递值之前,应先声明并初始化 City 的实例。City city;//....city = new City//...city.setId(cities.get(position));这样您就可以再次测试您的代码。
随时随地看视频慕课网APP

相关分类

Java
我要回答