2013-08-16 66 views
0

我想设置一个列表视图只有一些图像,即没有文字。下面是我的文件:ImageView中的列表空指针异常

//the Main File: 
    public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ListView listview1 = (ListView)findViewById(R.id.listView1); 
     final Integer[] mThumbIds = { 
       R.drawable.blue, R.drawable.blue2, 
       R.drawable.blue3, R.drawable.blue4, 
       R.drawable.blue5, R.drawable.blue6 
     }; 
     listview1.setAdapter(new ImageAdapter(this,mThumbIds)); 
     listview1.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
       //Upon Clicking 
      } 
     }); 
    } 

    @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; 
    } 
    class ImageAdapter extends ArrayAdapter<Integer> { 
     private Context mContext; 
     Integer [] resources; 
     public ImageAdapter (Context c, Integer[] resources) { 
      super(c, R.layout.activity_main, resources); 
      //System.out.println("Set up of Image Adapter"); 
      mContext = c; 
      this.resources=resources; 
      for(Integer resource:resources){ 
       System.out.println(resource); 
      } 
     } 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View rowView = inflater.inflate(R.layout.activity_main, parent, false); 
      ImageView imageView = (ImageView) rowView.findViewById(R.layout.image_view); 
      imageView.setImageResource(resources[position]); 
      return rowView; 
     }  
    } 
    } 

的XML如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/export_folder_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="Formation Name:" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 
    <ListView 
     android:id="@+id/listView1"   
     android:layout_below="@+id/color_textView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:columnWidth="90dp" 
     android:numColumns="auto_fit" 
     android:verticalSpacing="10dp" 
     android:horizontalSpacing="10dp" 
     android:stretchMode="columnWidth" 
     android:gravity="center" 
     android:background="#ff000000" > 
    </ListView> 
</RelativeLayout> 

最后,一个定义的ImageView:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_vertical" 
    android:minHeight="64dp"> 
    <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="32dp" 
      android:layout_height="32dp" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="9dp" 
      android:layout_alignParentTop="true"/> 
</RelativeLayout> 

不幸的是,我得到的地步,NullPointerException异常我在图像视图上设置了图像资源。图像资源存在 - 我确定了。 我不确定我做错了什么,而我是Android新手。谢谢。的

回答

2

看起来你是在你的getView()充气错误的XML,

View rowView = inflater.inflate(R.layout.activity_main, parent, false); 

这里R.layout.activity_main应该由它包含ImageView的xml文件来代替。

从你的代码,我认为包含ImageView的XML文件的名称是,R.layout.image_view

所以,你的最终代码应该是这样的,

View rowView = inflater.inflate(R.layout.image_view, parent, false); 
      ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView); 
      imageView.setImageResource(resources[position]); 
+0

Downvoter!介意解释你的行为? –

+0

我认为这是例外..我不知道为什么一些人投下了这个所以+ +1 –

+0

@ArunCThomas感谢您验证我的观点.. :) –

0

使用R.id.image_view而是采用R.layout.image_view 更改行

ImageView imageView = (ImageView) rowView.findViewById(R.layout.image_view); 

以下

线
ImageView imageView = (ImageView) rowView.findViewById(R.id.image_view); 
+0

图像视图实际上命名ImageView的。否则你的答案是完全正确的。请参阅上面的确切答案。谢谢。 –

1

试试下面的代码link: -

公共查看getView(INT位置,查看convertView ,ViewGroup父){

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View rowView = inflater.inflate(rowResourceId, parent, false); 
    ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView); 


    int id = Integer.parseInt(Ids[position]); 
    String imageFile = Model.GetbyId(id).IconFile; 


    // get input stream 
    InputStream ims = null; 
    try { 
     ims = context.getAssets().open(imageFile); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    // load image as Drawable 
    Drawable d = Drawable.createFromStream(ims, null); 
    // set image to ImageView 
    imageView.setImageDrawable(d); 
    return rowView; 

} 

}

+0

为什么downvoter ??? –

+0

不知道谁或为什么@Yogesh。谢谢你,伙计,这是完全正确的。 –