Search code examples
androidlistviewandroid-arrayadaptercustom-lists

Issue with Custom Adapter


I was trying to use a Custom adapter extending Array adapter but I am getting a null pointer Exception Here i am posting my Code please go through that

Note: I should not use XML

my mainActivity is

package com.example.newslist;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;

public class MainActivity extends Activity {
DisplayMetrics dm = new DisplayMetrics();
public static int width,height;
public String text="1";
int image = R.drawable.ic_launcher;
public static ArrayList<ItemClass> listWithImage ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        width=dm.widthPixels;
        height = dm.widthPixels;
        listWithImage=new ArrayList<ItemClass>();


        LinearLayout mainLayout = new LinearLayout(this);
        mainLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        setContentView(mainLayout);

        ListView list1 = new ListView(this);


        ItemClass item = new ItemClass(text, image);
        listWithImage.add(item);
        list1.setAdapter(new CustomAdapter(MainActivity.this,listWithImage));
        list1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, (int) (height*0.4)));
        mainLayout.addView(list1);
        }

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

}

and my Custom adapter is

package com.example.newslist;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class CustomAdapter extends ArrayAdapter<ItemClass>{
    Context ctx;
    static LinearLayout CustomLayout;
    static TextView Name;       
    ImageView Image;
    public CustomAdapter(Context context, List<ItemClass> objects) {
        super(context, CustomLayout.getId(),Name.getId(), objects);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ItemClass list = this.getItem(position);

    CustomLayout = new LinearLayout(ctx);
    CustomLayout.setId(2);
    CustomLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
    CustomLayout.setOrientation(LinearLayout.VERTICAL);

    Image = new ImageView(ctx);
    Image.setLayoutParams(new LayoutParams((int) (MainActivity.width*0.006),(int )(MainActivity.height*0.006)));
    Image.setImageResource(R.drawable.ic_launcher);
    CustomLayout.addView(Image);

    Name = new TextView(ctx);
    Name.setId(1);
    Name.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    Name.setPadding((int) (MainActivity.width*0.0005),(int )(MainActivity.height*0.0005),(int) (MainActivity.width*0.0005),(int )(MainActivity.height*0.0005));
    CustomLayout.addView(Name);

    Image.setImageResource(list.getImage());
    Name.setText(""+list.getName());

    return CustomLayout;




    }


}

and my item class is

package com.example.newslist;

public class ItemClass {
    public int imageId;
    private String name;
    public ItemClass(String text, int image)
    {
        this.name = text;
        this.imageId = image;
    }
    public String getName()
    {
        return name;
    }
    public int getImage()
    {
        return imageId;
    }

}

Solution

  • The Textview Name is accesed before assiging a value to it. That is causing the null pointer exception.Instead of Name.getId() specify the resource id of your custom layout like R.layout.CUSTOM_LAYOUT
    The Context ctx is not getting any value assigned, it will cause a null pointer exception. Try the code below

    public class CustomAdapter extends ArrayAdapter<ItemClass>{
    Context ctx;
    static LinearLayout CustomLayout;
    static TextView Name;       
    ImageView Image;
    public CustomAdapter(Context context, List<ItemClass> objects) {
        super(context, CustomLayout.getId(),R.layout.custom_adapter_view, objects);
        ctx = context;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        final ItemClass list = objects.getItem(position);
    
    CustomLayout = new LinearLayout(ctx);
    CustomLayout.setId(2);
    CustomLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
    CustomLayout.setOrientation(LinearLayout.VERTICAL);
    
    Image = new ImageView(ctx);
    Image.setLayoutParams(new LayoutParams((int) (MainActivity.width*0.006),(int )(MainActivity.height*0.006)));
    Image.setImageResource(R.drawable.ic_launcher);
    CustomLayout.addView(Image);
    
    Name = new TextView(ctx);
    Name.setId(1);
    Name.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    Name.setPadding((int) (MainActivity.width*0.0005),(int )(MainActivity.height*0.0005),(int) (MainActivity.width*0.0005),(int )(MainActivity.height*0.0005));
    CustomLayout.addView(Name);
    
    Image.setImageResource(list.getImage());
    Name.setText(""+list.getName());
    
    return CustomLayout;
    
    
    
    
    }
    

    }