0

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

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.