自定义适配器问题

时间:2013-11-14 12:11:00

标签: android listview android-arrayadapter custom-lists

我试图使用自定义适配器扩展数组适配器,但我得到一个空指针异常 在这里,我发布我的代码,请完成

注意:我不应该使用XML

我的mainActivity是

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

}

我的自定义适配器

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;




    }


}

我的项目类是

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

}

2 个答案:

答案 0 :(得分:0)

您没有初始化变量CustomLayout和Name,所以当您调用CustomLayout.getId()Name.getId()时,它返回null,超类的构造函数不会接受它,因此抛出NullPointer。

编辑:尝试使用此构造函数:

public CustomAdapter(Context context, List<ItemClass> objects) {
    super(context, android.R.layout.simple_list_item_1, android.R.id.text1, objects);
    ctx = context;
}

答案 1 :(得分:0)

在为其分配值之前,已加入 Textview名称。这导致空指针异常。而不是 Name.getId()指定自定义布局的资源ID,如 R.layout.CUSTOM_LAYOUT
上下文< strong> ctx 未分配任何值,它将导致空指针异常。请尝试以下代码

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;




}

}