Question

public View getView(int position, View convertView, ViewGroup parent) {         
    ViewHolder viewHolder = null;
    View vi= convertView;

    if(convertView==null){
        viewHolder = new ViewHolder();
        vi = inflater.inflate(R.layout.list_row, null);
        viewHolder.content = (TextView)vi.findViewById(R.id.f_content);
        viewHolder.cat_id  = (TextView)vi.findViewById(R.id.c_id);
        viewHolder.fact_id = (TextView)vi.findViewById(R.id.f_id);
        vi.setTag(viewHolder); 
    }else{
        viewHolder = (ViewHolder) convertView.getTag();
    }   

    HashMap<String, String> fact = new HashMap<String, String>();
    fact = data.get(position);

    // Setting all values in listview
    viewHolder.fact_id.setText(fact.get("_id"));
    viewHolder.cat_id.setText(fact.get("cat_id"));
    viewHolder.content.setText(fact.get("fact_content"));

    return vi;
}

I have created a LazyAdapter that extends a BaseAdaper to use with ListView.

The this adapter populates data in ListView. When I click on first 4 visible list items it prints the value of that item. Problem is when I scroll down the list view and click on item 5. The app crashes saying null pointer exception. Log trace is here for reference.

05-13 23:10:43.943: E/AndroidRuntime(3360): FATAL EXCEPTION: main 05-13 23:10:43.943: E/AndroidRuntime(3360): java.lang.NullPointerException 05-13 23:10:43.943: E/AndroidRuntime(3360): at com.example.facts.MainActivity$3.onItemClick(MainActivity.java:100) 05-13 23:10:43.943: E/AndroidRuntime(3360): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 05-13 23:10:43.943: E/AndroidRuntime(3360): at android.widget.ListView.performItemClick(ListView.java:3569) 05-13 23:10:43.943: E/AndroidRuntime(3360): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1831) 05-13 23:10:43.943: E/AndroidRuntime(3360): at android.os.Handler.handleCallback(Handler.java:587) 05-13 23:10:43.943: E/AndroidRuntime(3360): at android.os.Handler.dispatchMessage(Handler.java:92) 05-13 23:10:43.943: E/AndroidRuntime(3360): at android.os.Looper.loop(Looper.java:150) 05-13 23:10:43.943: E/AndroidRuntime(3360): at android.app.ActivityThread.main(ActivityThread.java:4389) 05-13 23:10:43.943: E/AndroidRuntime(3360): at java.lang.reflect.Method.invokeNative(Native Method) 05-13 23:10:43.943: E/AndroidRuntime(3360): at java.lang.reflect.Method.invoke(Method.java:507) 05-13 23:10:43.943: E/AndroidRuntime(3360): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 05-13 23:10:43.943: E/AndroidRuntime(3360): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 05-13 23:10:43.943: E/AndroidRuntime(3360): at dalvik.system.NativeStart.main(Native Method)

The Onclick Methods is like this:

// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {

    RelativeLayout node = (RelativeLayout)parent.getChildAt(position);
    TextView fact = (TextView) node.findViewById(R.id.c_id);
    Log.d("ListItem Clicked", "id: " + id + " position: "
        + position + " artist " + fact.getText());
}
});

Was it helpful?

Solution

My suggestion would be to revisit your onItemClickListener implementation.

You should not need to access parent at all for what you are trying to do. Try this.

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

        TextView fact = (TextView) view.findViewById(R.id.c_id);
        Log.d("ListItem Clicked", "id: " + id + " position: "
        + position + " artist " + fact.getText());
    }
});

The view argument supplied in the method is actually the view being click on.

view; The view within the AdapterView that was clicked (this will be a view provided by the adapter)

Please refer to AdapterView.OnItemClickListener for more details.

The parent is there to help you identified which AdapterView that is being acted on. For example, say if you have two ListAdapter but only want to use one listener then it going to help you specify which is the one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top