Android view.findViewById(R.id…)和just findViewById(R.id…)之间的差异
我正在尝试实现一个自定义阵列适配器 问题是我的代码在使用时崩溃Android view.findViewById(R.id…)和just findViewById(R.id…)之间的差异,android,android-view,findviewbyid,Android,Android View,Findviewbyid,我正在尝试实现一个自定义阵列适配器 问题是我的代码在使用时崩溃 ImageView imageView=(ImageView)findViewById(R.id.imageView); 而不是 ImageView imageView=(ImageView)rowView.findViewById(R.id.imageView); 其中rowView是我已导入的ListView的布局文件 为什么会出现这种情况,我想rowView.findViewById(R.id…)只会加快按id查找元素的
ImageView imageView=(ImageView)findViewById(R.id.imageView);
而不是
ImageView imageView=(ImageView)rowView.findViewById(R.id.imageView);
其中rowView是我已导入的ListView的布局文件
为什么会出现这种情况,我想rowView.findViewById(R.id…)只会加快按id查找元素的速度,但如果没有它,应用程序就会崩溃,
你能解释一下吗
这是我的活动代码
公共类MainActivity扩展了AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Now for the list view
ListView listView=(ListView)findViewById(R.id.listView);
String[] values =new String[]{"iOS","android","firefoxOs","Ubuntu"};
MySimpleAdapter simpleAdapter=new MySimpleAdapter(this,values);
listView.setAdapter(simpleAdapter);
//
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class MySimpleAdapter extends ArrayAdapter<String>{
private Context context;
private String[] values;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//listView calls this on its adapter for each row
Log.v("list","getView");
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView=inflater.inflate(R.layout.custom_row,parent,false);
TextView textView1= (TextView) rowView.findViewById(R.id.textView1);
textView1.setText(values[position]);
//for the android row alone set an image , others leave it as blank
if(values[position].startsWith("and"))
{
ImageView imageView=(ImageView)rowView.findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.ic_launcher);//Ok R.drawale....is also an id
}
return rowView;
}
public MySimpleAdapter(Context context,String[] values) {
super(context, R.layout.custom_row,values);
this.context=context;
this.values=values;
Log.v("list","constructor");
}
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//现在进入列表视图
ListView ListView=(ListView)findViewById(R.id.ListView);
字符串[]值=新字符串[]{“iOS”、“android”、“firefoxOs”、“Ubuntu”};
mysimpledapter simpledapter=新的mysimpledapter(这个,值);
setAdapter(simpleAdapter);
//
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
类MySimpleAdapter扩展了ArrayAdapter{
私人语境;
私有字符串[]值;
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//listView在其适配器上为每一行调用它
Log.v(“列表”、“获取视图”);
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
视图行视图=充气机。充气(R.layout.custom_行,父,false);
TextView textView1=(TextView)rowView.findViewById(R.id.textView1);
textView1.setText(值[位置]);
//对于android行单独设置图像,其他行将其保留为空白
if(值[position].startsWith(“and”))
{
ImageView ImageView=(ImageView)rowView.findViewById(R.id.ImageView);
setImageResource(R.drawable.ic_launcher);//确定R.drawale…也是一个id
}
返回行视图;
}
public mysimpledapter(上下文,字符串[]值){
super(上下文、右布局、自定义行、值);
this.context=context;
这个。值=值;
Log.v(“列表”、“构造函数”);
}
}
}
findViewById()
用于在活动的布局中查找视图。例如,这在片段中是不可能的<代码>视图。findViewById()
用于在特定的其他视图中查找视图。例如,在列表视图
行布局中查找视图。您的应用程序在没有它的情况下崩溃,因为您正在搜索的视图位于rowView
中。使用普通的findViewById()
无法找到视图。findViewById始终需要上下文才能找到视图
如果您是从一个扩展了Activity
类的类调用此函数,则可以使用findViewById
,因为Activity是上下文
当您从扩展了fragment
的类调用它时,必须使用getActivity()
在您的情况下,由于是在适配器中调用它
,因此需要在列表视图行
中找到该视图。因此,您可以使用查看。findViewById
我现在编辑了这个问题,您可以详细说明吗?好的,我明白了,因为我的内部类不扩展活动,默认情况下没有this(context)。findViewById(…),所以存在问题,我说的对吗?但是从您所说的来看,当我在没有上下文的情况下使用findViewById时,为什么没有IDE警告或编译时错误?顺便说一句,我的CustomAdapter类是扩展活动的主活动的内部类…你能解释一下吗?是的,这就是问题所在。但我想知道如何在适配器中使用findviewbyid而不出现编译错误。我说过它是一个内部类,我现在就把整个代码放进去