從我的理解,你正試圖添加一個垂直linearlayout圖像列表?
我建議您使用帶有自定義佈局的ListView和ArrayAdapter。
下面是一個示例代碼,我做來幫助你:
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymain);
StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
List<Bitmap> bitmaps= new ArrayList<Bitmap>();
for(int i = 1; i < 12; i++) {
try {
URL imageURL = new URL("http://developer.android.com/assets/images/dac_logo.png");
Bitmap image = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());
bitmaps.add(image);
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
}
setListAdapter(new BitmapAdapter(this,R.layout.listview_item,bitmaps));
}
定製BitmapAdapter:
public class BitmapAdapter extends ArrayAdapter<Bitmap>{
Context context;
int layoutResourceId;
List<Bitmap> bitmaps;
public BitmapAdapter(Context context, int textViewResourceId,
List<Bitmap> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.layoutResourceId = textViewResourceId;
this.bitmaps = objects;
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(this.layoutResourceId, parent, false);
ImageView iv = (ImageView) rowView.findViewById(R.id.imageView1);
iv.setImageBitmap(this.bitmaps.get(position));
return rowView;
}
}
activitymain.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list">
</ListView>
listview_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
在'bitmaps.get(i)'上添加'null'檢查並檢查。我猜你的'bitmaps'爲空。 –
嘗試給ImageView提供適當的大小,它可能會崩潰。 –
@MMohsinNaeem謝謝!我檢查了一下,然後我認爲出了什麼問題:應該產生圖像的PHP頁面出錯了。感謝幫助! – MikkoP