views:

477

answers:

1

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??

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<String> VTitle;
    Vector<String> 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<?> arg0) {
        // TODO Auto-generated method stub
    }
}

And the xmls are like this....

"main.xml"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id = "@+id/display"
        android:text="something"
        />
    <ListView 
         android:id="@android:id/list" 
         android:layout_width="fill_parent" 
         android:drawSelectorOnTop="false" 
        android:layout_height="0px">
    </ListView>
</LinearLayout>

"row.xml"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView 
        android:id="@+id/image" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">    
    </ImageView>
    <TextView 
        android:text="Title" 
        android:id="@+id/title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </TextView>
    <TextView 
        android:text="description" 
        android:id="@+id/description" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </TextView>
</LinearLayout>
+2  A: 

VTitle and VDescription aren't initialized

Before accessing one of these attributes, you should :

VTitle = new Vector<String>();
VDescription = new Vector<String>();

Moreover in java, the first letter of an attribute name should be lowercase, and in android this first letter should be an m, to denote a member field.

kosokund