2

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;
    }

}
4
  • At which line do you get the exception ?
    – 2Dee
    Nov 14, 2013 at 12:12
  • super(context, CustomLayout.getId(),Name.getId(), objects); in the custom adapter
    – Hummer
    Nov 14, 2013 at 12:12
  • and even list1.setAdapter(new CustomAdapter(MainActivity.this,listWithImage)); in main activity
    – Hummer
    Nov 14, 2013 at 12:13
  • post your logcat here...
    – Piyush
    Nov 14, 2013 at 12:23

2 Answers 2

0

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;




}

}

2
  • 1
    I am using not even a single layout
    – Hummer
    Nov 14, 2013 at 12:41
  • then specify the id of the textview
    – SathMK
    Nov 14, 2013 at 12:44
0

You are not initializing variables CustomLayout and Name so when you call CustomLayout.getId() and Name.getId(), it returns null which will not be accepted by the constructor of the super class, hence throwing a NullPointer.

EDIT : try using this constructor :

public CustomAdapter(Context context, List<ItemClass> objects) {
    super(context, android.R.layout.simple_list_item_1, android.R.id.text1, objects);
    ctx = context;
}
3
  • Could you please tell me that I should do
    – Hummer
    Nov 14, 2013 at 12:20
  • You could use android.R.layout.simple_list_item_1 and android.R.id.text1 instead of getting Ids from Views that have not been created.
    – 2Dee
    Nov 14, 2013 at 12:42
  • Can someOne please give me an example to use ArrayAdapter without using XML
    – Hummer
    Nov 14, 2013 at 12:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.