2

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

注意:我不应该使用 XML

我的主要活动是

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

}
4

2 回答 2

0

您没有初始化变量 CustomLayout 和 Name ,因此当您调用CustomLayout.getId()and时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;
}
于 2013-11-14T12:18:48.727 回答
0

在为其分配值之前,将访问 Textview 名称。这导致空指针异常。而不是 Name.getId()指定自定义布局的资源 id,如R.layout.CUSTOM_LAYOUT
上下文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;




}

}

于 2013-11-14T12:35:26.403 回答