Rajat Saxena
Rajat Saxena

Reputation: 3925

Android: getting null pointer exception while setAdapter()

I am very new to Android programming.I am trying to create a basic list app.I am geting nullpointer exception while setting adapter using setAdapter().My code for the same is as follows.

ListAdapter.java

package com.example.listexmpl;
import java.util.ArrayList;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter{

    private ArrayList<ListRecord> records = new ArrayList<ListRecord>();


    public ListAdapter(){
        records.add(new ListRecord("Rajat","I'm feeling very happy today."));
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return records.size();
    }

    @Override
    public Object getItem(int index) {
        // TODO Auto-generated method stub
        return getItem(index);
    }

    @Override
    public long getItemId(int index) {
        // TODO Auto-generated method stub
        return index;
    }

    @Override
    public View getView(int index, View view, ViewGroup parent) {
        // TODO Auto-generated method stub
        if(view==null){
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            view = inflater.inflate(R.layout.list_record,parent,false);
        }
        ListRecord listr = records.get(index);
        TextView nameView = (TextView) view.findViewById(R.id.name_view);
        TextView statView = (TextView) view.findViewById(R.id.stat_view);
        nameView.setText(listr.getName());
        statView.setText(listr.getStatus());
        return view;
    }

}

ListActivity.java

package com.example.listexmpl;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;

public class ListActivity extends Activity {

    ListAdapter lstrecordAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        ListView lstview = null;
        lstview = (ListView) findViewById(R.id.list_record);
        lstrecordAdapter = new ListAdapter();
        lstview.setAdapter(lstrecordAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_list, menu);
        return true;
    }

}

When I put the statement containing setAdapter() in try-catch block the app runs but there is nothing to see on screen except the app header.Where am I going wrong?

[edit] activity_list.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

</ListView>

Upvotes: 0

Views: 3358

Answers (1)

Blackbelt
Blackbelt

Reputation: 157457

you missed the id item for your ListView

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/listviewid"
    >

</ListView>

Inside the activity change

ListView lstview = null;
lstview = (ListView) findViewById(R.id.list_record);

with

 ListView lstview = null;
 lstview = (ListView) findViewById(R.id.listviewid);

Upvotes: 3

Related Questions