1 回答
TA贡献1725条经验 获得超7个赞
l1之前不能引用setContentView(R.layout.activity_namelist)
正确的代码在这里:
public class Namelist extends AppCompatActivity {
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};
ListView l1;
ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_namelist);
l1 = findViewById(R.id.list);
arrayAdapter = new ArrayAdapter<String>(Namelist.this,android.R.layout.simple_list_item_1,mobileArray);
l1.setAdapter(arrayAdapter);
}
}
并且您还将错误传递layout R.layout.activity_namelist给了 的第二个参数ArrayAdapter。您没有声明布局名称activity_namelist。因此,您可以使用默认的内置布局android.R.layout.simple_list_item_1询问上面的代码。
添加回答
举报