findViewById返回null。但为什么?

时间:2013-01-16 14:24:54

标签: android eclipse findviewbyid

我有一个Adroid Activity,我想介绍一个自定义列表视图。这是我的onCreate()活动方法。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LoadListView();

    ListView lvw = (ListView)findViewById(android.R.id.list);

    lvw.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

            // selected item
            String element = ((TextView) view).getText().toString();

            // Launching new Activity on selecting single List Item
            Intent i = new Intent(getApplicationContext(), AddEntryActivity.class);
            // sending data to new activity
            i.putExtra("entryToEdit", element);
            long idToEdit = Long.parseLong(hEntries.get(position).toString());
            i.putExtra("idToEdit", idToEdit);
            startActivity(i);

        }
      });   

    DbAccess oDb = new DbAccess(this);
}

如您所见,调用LoadListView()方法来加载listview项目:

private void LoadListView()
{
    oDb = new DbAccess(this);
    oDb.open();

    List<cAttivita> entries = oDb.getAttivita();

    CustomListAdapter listAdapter = new CustomListAdapter(MainActivity.this, android.R.layout.simple_list_item_1 , entries);
    ListView lvwAttivita = (ListView)findViewById(android.R.id.list);
    lvwAttivita.setAdapter(listAdapter);

    hEntries = new HashMap();

    for(int i = 0; i < entries.size(); i++)
    {
        hEntries.put(i, entries.get(i).getId());
    }
}

CustomListAdapter类:

public CustomListAdapter(Context context, int textViewResourceId , List<cAttivita> list ) 
{
    super(context, textViewResourceId, list);           
    mContext = context;
    id = textViewResourceId;
    items = list ;
}

@Override
public View getView(int position, View v, ViewGroup parent)
{
    View mView = v ;
    if(mView == null){
        LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = vi.inflate(id, null);
    }

    TextView text = (TextView) mView.findViewById(R.id.textView);

    if(items.get(position) != null )
    {
        text.setTextColor(Color.WHITE);
        text.setText(items.get(position).toString());
        text.setBackgroundColor(Color.RED); 
        int color = Color.argb( 200, 255, 64, 64 );
            text.setBackgroundColor( color );

    }           

    return mView;
}

这是activity_main.xml代码:

 <TextView android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView" 
    android:textSize="20px" 
    android:paddingTop="10dip" 
    android:paddingBottom="10dip"/> 

    <ListView xmlns:android="schemas.android.com/apk/res/android"; 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Attività"/> 

问题在于:

TextView text = (TextView) mView.findViewById(R.id.textView);

为什么text会返回null?

1 个答案:

答案 0 :(得分:4)

在CustomAdapter的getView()中更改以下代码行。

TextView text = (TextView) mView.findViewById(android.R.id.text1);

当您将android.R.layout.simple_list_item_1作为布局时,它应该是android.R.id.text1,它在Android库中内部声明。

在getView()中更改我的代码行,它适用于你。