view.findViewById(R.id...) 和 findViewById(R.id...) 之间的区别

Difference between view.findViewById(R.id....) and just findViewById(R.id...)

我正在尝试实现自定义数组适配器,

问题是我的代码在使用时崩溃

 ImageView imageView=(ImageView)findViewById(R.id.imageView);

而不是

ImageView imageView=(ImageView)rowView.findViewById(R.id.imageView);

其中 rowView 是我输入的 ListView 的布局文件。

为什么会发生这种情况,我认为 rowView.findViewById(R.id..) 只会使通过 id 查找元素的速度更快,但没有它应用程序会崩溃, 你能解释一下吗

这是我的Activity代码

public class MainActivity extends 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");
    }
}

}

findViewById() 用于在 Activity 的布局中查找视图。例如,这在 Fragment 中是不可能的。 view.findViewById() 用于在特定的其他视图中查找视图。例如,在 ListView 行布局中查找视图。如果没有它,您的应用程序会崩溃,因为您要搜索的视图在 rowView 内。通过使用正常 findViewById(),您将找不到视图。

findViewById 始终需要上下文来查找视图。

如果您从扩展 Activity class 的 class 调用它,您可以只使用 findViewById,因为 Activity 是上下文。

当您从扩展 fragment 的 class 调用它时,您必须使用 getActivity().findViewById

在您的情况下,由于您是在 adapter 中调用它,因此您需要在 listview row 中找到该视图。所以你使用 view.findViewById