Note that there are some explanatory texts on larger screens.

plurals
  1. POListActivity throws NullPointerException
    text
    copied!<p>Hey guys i have a ListActivity... a very simple at that... and it keeps throwing NullPointer Exception though i have done it exactly as the Sample List7 except that i have used the Layout inflater... below is the code... Can u plz comment the error i have done here??</p> <pre><code>import java.util.Vector; import android.app.Activity; import android.app.ListActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.OnItemSelectedListener; public class CustomList extends ListActivity implements OnItemSelectedListener{ Vector&lt;String&gt; VTitle; Vector&lt;String&gt; VDescription; TextView display; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); VTitle.addElement("First Title"); VTitle.addElement("Second Title"); VTitle.addElement("Third Title"); VTitle.addElement("Fourth Title"); VDescription.addElement("1 Description"); VDescription.addElement("2 Description"); VDescription.addElement("3 Description"); VDescription.addElement("4 Description"); display = (TextView)findViewById(R.id.display); setListAdapter(new CustomAdapter(this)); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub display.setText(VTitle.elementAt(position)); } class CustomAdapter extends BaseAdapter { protected Activity mContext; public CustomAdapter(Activity context) { mContext = context; // TODO Auto-generated constructor stub } @Override public int getCount() { // TODO Auto-generated method stub return VTitle.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View row = convertView; if(row==null) { LayoutInflater inflater = mContext.getLayoutInflater(); row = inflater.inflate(R.layout.row,null); } TextView title = (TextView)row.findViewById(R.id.title); title.setText(VTitle.elementAt(position)); TextView description = (TextView)row.findViewById(R.id.description); description.setText(VDescription.elementAt(position)); ImageView image = (ImageView)row.findViewById(R.id.image); switch(position){ case 0: image.setImageResource(R.drawable.check); break; case 1: image.setImageResource(R.drawable.dos); break; case 2: image.setImageResource(R.drawable.smily); break; case 3: image.setImageResource(R.drawable.wrong); break; } return(row); } } @Override public void onItemSelected(AdapterView parent, View v, int position, long id) { // TODO Auto-generated method stub display.setText(VTitle.elementAt(position)); } @Override public void onNothingSelected(AdapterView&lt;?&gt; arg0) { // TODO Auto-generated method stub } } </code></pre> <p>And the xmls are like this....</p> <p>"main.xml"</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id = "@+id/display" android:text="something" /&gt; &lt;ListView android:id="@android:id/list" android:layout_width="fill_parent" android:drawSelectorOnTop="false" android:layout_height="0px"&gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; </code></pre> <p>"row.xml"</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt; &lt;ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt; &lt;/ImageView&gt; &lt;TextView android:text="Title" android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt; &lt;/TextView&gt; &lt;TextView android:text="description" android:id="@+id/description" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt; &lt;/TextView&gt; &lt;/LinearLayout&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload