我尝试将ListView与BaseAdapter一起使用。我在这一行上有一个 java.lang.NullPointerException :
titolo.setText(getResources().getString(R.string.nome_cognome_utente, p.nome));
我做错了什么?
这是我的主要活动:
public class ListaUtenti extends Activity {
private ListView listView;
private ListAdapter adapter;
private List<Persona> persona = new LinkedList<Persona>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_utenti);
listView = (ListView)findViewById(R.id.listaUtenti);
adapter = new BaseAdapter() {
@Override
public int getCount() {
return persona.size();
}
@Override
public Persona getItem(int position) {
return persona.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
if(view == null){
view = getLayoutInflater().inflate(R.layout.list_item_persona, null);
}
final TextView titolo = (TextView)findViewById(R.id.firstLine);
final TextView descrizione = (TextView)findViewById(R.id.secondLine);
final Persona p = getItem(position);
titolo.setText(getResources().getString(R.string.nome_cognome_utente, p.nome));
descrizione.setText(getResources().getString(R.string.descrizione, p.cognome));
return view;
}
};
listView.setAdapter(adapter);
}
@Override
protected void onStart(){
super.onStart();
persona.clear();
persona.add(new Persona("nome1","cognome1"));
persona.add(new Persona("nome2","cognome2"));
persona.add(new Persona("nome3","cognome3"));
listView.setAdapter(adapter);
}
}
这是xml文件activity_lista_utenti中的ListView:
<ListView
android:id="@+id/listaUtenti"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
tools:listitem="@android:layout/simple_list_item_1" />
这是xml文件list_item_persona:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_margin="5dp"
android:src="@drawable/ic_contact_picture"
android:contentDescription="@string/immagine_profilo" />
<TextView
android:id="@+id/firstLine"
android:layout_width="fill_parent"
android:background="@color/yellow2"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/icon"
android:layout_toEndOf="@id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center_vertical"
android:textSize="20sp" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:background="@color/yellow"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_marginTop="35dp"
android:layout_toRightOf="@id/icon"
android:layout_toEndOf="@id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center_vertical"
android:textSize="15sp" />
</RelativeLayout>
答案 0 :(得分:3)
从view的findViewById获取textview,即。 变化
final TextView titolo = (TextView)findViewById(R.id.firstLine);
final TextView descrizione = (TextView)findViewById(R.id.secondLine);
到
final TextView titolo = (TextView)view.findViewById(R.id.firstLine);
final TextView descrizione = (TextView)view.findViewById(R.id.secondLine);
因为它们是list_item_persona
视图的一部分。