我编写了一个代码,其目标是将数组列表项设置为回收视图。但是当我运行该项目时,应用程序将崩溃。d.我检查依赖关系。他们没事。我检查了代码,我认为它们没有任何问题。//主活动类public class MainActivity extends AppCompatActivity {RecyclerView rcl_cities;@Overrideprotected 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;}}
1 回答
哔哔one
TA贡献1854条经验 获得超8个赞
首先,将您的更改view为xmlView
<View
android:layout_width="match_parent"
android:layout_height="3dp"
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));
这样您就可以再次测试您的代码。
添加回答
举报
0/150
提交
取消