2013-06-21 20 views
-1

我想從可繪製文件夾中的圖像列表動態創建自定義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; 
} 


} 

這裏是跟蹤目錄下載:

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

發表您的堆棧跟蹤 – Raghunandan

+0

什麼的就行101'ImageAdapter'類? – Raghunandan

+0

images [index ++] = list [i] .getInt(null);這是你面臨的問題? – SKK

回答

0

在getView中,使用myContext代替活動。嘗試,

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"); 
+0

謝謝,它的工作。 – user2435182

+0

我歡迎.. :) – Nizam

0

嗯,NPE是上線101這應該是

Bitmap thumb = BitmapFactory.decodeResource(myContext.getResources(), images[position], options); 

我猜測你的上下文由於某種原因是空的,因爲它是你從構造函數獲得的唯一引用,並且不會創建你自己。嘗試使用getApplicationContext()。getResources()。

+0

'myContext'在ImageAdapter類的構造函數中初始化 – Raghunandan

+0

它不是 - 它從調用活動傳遞。它確實不應該是空的,但其他參數看起來很清楚地分配給我。也許提問者應該分開陳述,以便每一個論點都在自己的路線上,那麼它將清楚哪一個是拋NPE – npace

相關問題