2013-06-21 2 views
-1

나는 드로어 블 폴더의 이미지 목록에서 사용자 지정 ListView를 동적으로 만들고 싶지만 null 포인터 예외가 발생합니다. 누군가 내 코드에 문제가있는 것을 발견 할 수 있습니까? 여기에 내 코드 ... 여기이미지 및 텍스트 사용자 지정 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

스택 추적을 게시하십시오 – Raghunandan

+0

무엇이 온라인 101' ImageAdapter' 클래스에 있습니까? – Raghunandan

+0

이미지 [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); 

컨텍스트가 어떤 이유로 든 null 인 것은 추측합니다.이 컨텍스트가 생성자에서 가져와 자신을 만들지 않는 유일한 참조이기 때문입니다. getApplicationContext(). getResources()를 사용해보십시오.

+0

ImageAdapter 클래스의 생성자에서 'myContext'가 초기화되었습니다. – Raghunandan

+0

그것은 호출하는 액티비티에서 전달되었습니다. 그것은 정말로 null이 아니어야합니다. 그러나 다른 매개 변수는 분명히 나에게 할당되어 있습니다. 질문자는 각 인수가 자체 줄에 있도록 문장을 분리해야합니다. 그러면 어떤 것이 NPE를 던지고 있는지 분명해질 것입니다. – npace

관련 문제