我想从drawable文件夹中的图像列表动态创建自定义ListView,但我得到空指针异常。有人可以找到我的代码有什么问题。这是我的代码......
public class ImageSelectionActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ImageAdapter(this));
}
@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;
}
}
public class ImageAdapter extends BaseAdapter {
// a list of resource IDs for the images we want to display
private Integer[] images;
// a context so we can later create a view within it
private Context myContext;
// store a cache of resized bitmaps
// Note: we're not managing the cache size to ensure it doesn't
// exceed any maximum memory usage requirements
private Bitmap[] cache;
private static LayoutInflater inflater=null;
private Activity activity;
// Constructor
public ImageAdapter(Context c) {
myContext = c;
// Dynamically figure out which images we've imported
// into the drawable folder, so we don't have to manually
// type each image in to a fixed array.
// obtain a list of all of the objects in the R.drawable class
Field[] list = R.drawable.class.getFields();
int count = 0, index = 0, j = list.length;
// We first need to figure out how many of our images we have before
// we can request the memory for an array of integers to hold their contents.
// loop over all of the fields in the R.drawable class
for(int i=0; i < j; i++)
// if the name starts with img_ then we have one of our images!
if(list[i].getName().startsWith("puzzle_")) count++;
// We now know how many images we have. Reserve the memory for an
// array of integers with length 'count' and initialize our cache.
images = new Integer[count];
cache = new Bitmap[count];
try {
for(int i=0; i < j; i++)
if(list[i].getName().startsWith("puzzle_")){
images[index++] = list[i].getInt(null);
}
} catch(Exception e) {}
}
@Override
// the number of items in the adapter
public int getCount() {
return images.length;
}
@Override
// not implemented, but normally would return
// the object at the specified position
public Object getItem(int position) {
return null;
}
@Override
// return the resource ID of the item at the current position
public long getItemId(int position) {
return images[position];
}
// create a new ImageView when requested
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgView = null;
TextView textView;
View itemView = convertView;
if(itemView == null) {
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.item_view, null);
imgView = (ImageView)itemView.findViewById(R.id.img_item);
textView = (TextView)itemView.findViewById(R.id.text_item);
textView.setText("Hello");
}
// see if we've stored a resized thumb in cache
if(cache[position] == null) {
// create a new Bitmap that stores a resized
// version of the image we want to display.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap thumb = BitmapFactory.decodeResource(myContext.getResources(), images[position], options);
// store the resized thumb in a cache so we don't have to re-generate it
cache[position] = thumb;
}
// use the resized image we have in the cache
imgView.setImageBitmap(cache[position]);
return itemView;
}
}
以下是CatLog跟踪:
06-21 13:00:12.738: W/dalvikvm(30020): threadid=1: thread exiting with uncaught exception (group=0x413702a0)
06-21 13:00:12.743: E/AndroidRuntime(30020): FATAL EXCEPTION: main
06-21 13:00:12.743: E/AndroidRuntime(30020): java.lang.NullPointerException
06-21 13:00:12.743: E/AndroidRuntime(30020): at com.binay.project.ImageAdapter.getView(ImageAdapter.java:101)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.widget.AbsListView.obtainView(AbsListView.java:2472)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.widget.ListView.makeAndAddView(ListView.java:1775)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.widget.ListView.fillDown(ListView.java:678)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.widget.ListView.fillFromTop(ListView.java:739)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.widget.ListView.layoutChildren(ListView.java:1628)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.widget.AbsListView.onLayout(AbsListView.java:2307)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.view.View.layout(View.java:14072)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.view.ViewGroup.layout(ViewGroup.java:4657)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.view.View.layout(View.java:14072)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.view.ViewGroup.layout(ViewGroup.java:4657)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.view.View.layout(View.java:14072)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.view.ViewGroup.layout(ViewGroup.java:4657)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.view.View.layout(View.java:14072)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.view.ViewGroup.layout(ViewGroup.java:4657)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2004)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1825)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1120)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4604)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.view.Choreographer.doFrame(Choreographer.java:525)
06-21 13:00:12.743: E/AndroidRuntime(30020): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
答案 0 :(得分:0)
嗯,NPE在第101行,应该是
Bitmap thumb = BitmapFactory.decodeResource(myContext.getResources(), images[position], options);
我猜你的上下文由于某种原因是空的,因为它是你从构造函数中获得的唯一引用,而不是自己创建的。尝试使用getApplicationContext()。getResources()。
答案 1 :(得分:0)
在getView中,使用myContext代替activity。尝试,
if(itemView == null) {
inflater = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.item_view, null);
}
imgView = (ImageView)itemView.findViewById(R.id.img_item);
textView = (TextView)itemView.findViewById(R.id.text_item);
textView.setText("Hello");