2013-08-16 67 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。謝謝你,夥計,這是完全正確的。 –