我想以编程方式添加几个图像视图,这取决于是否有要查看的图像..所以我在我的ArrayAdapter类中尝试过这个...我在这里做的是检查是否有任何额外的URL图像与否,然后创建一些imageview以显示这些图像
@Override
public View getView(int position, View rowView, ViewGroup parent) {
// TODO Auto-generated method stub
HeaderHolder headerRow = null;
View hs = null;
if (position == 0) {
if (hs == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.header, null);
LinearLayout linear =(LinearLayout) rowView.findViewById(R.id.headerlayout);
TextView main = (TextView) rowView.findViewById(R.id.headLine);
main.setText(appManager.getDetails().getTitle());
// Log.d("display width ", "" + display.getWidth());
// TextViewJustify.justifyText(main, display.getWidth() - 400f
// );
TextView pubDate = (TextView) rowView
.findViewById(R.id.pubdate);
pubDate.setText(appManager.getDetails().getPubDate());
TextView detailsBody = (TextView) rowView
.findViewById(R.id.detailsBody);
detailsBody.setText(appManager.getDetails().getBody());
// TextViewJustify.justifyText(detailsBody, display.getWidth() -
// 400f );
ImageView iv = (ImageView) rowView.findViewById(R.id.main);
String url = appManager.getDetails().getMainImageLink();
if (url != null) {
loader.fetchDrawableOnThread(appManager.getDetails()
.getMainImageLink(), iv);
}
if (appManager.getDetails().images.size() > 0) {
for (int i = 0; i < appManager.getDetails().images.size(); i++) {
im[i]= new ImageView(context);
im[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
im[i].setScaleType(ScaleType.FIT_XY);
linear.addView(im[i]);
loader.fetchDrawableOnThread(appManager.getDetails().images.get(i), im[i]);
}
}
ImageView play = (ImageView) rowView
.findViewById(R.id.videoYoutube);
if (appManager.getDetails().getVideos().size() > 0) {
play.setVisibility(View.VISIBLE);
play.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
context.startActivity(new Intent(
Intent.ACTION_VIEW, Uri.parse(appManager
.getDetails().getVideos().get(0))));
}
});
}
headerRow = new HeaderHolder();
headerRow.headLine = main;
headerRow.pubDate = pubDate;
headerRow.main = iv;
headerRow.lin = linear;
headerRow.details_images = im ;
headerRow.playButton = play;
hs = rowView;
hs.setTag(headerRow);
}
}
else if (position >= 1) {
..............
}
return rowView;
}
它始终在im[i]= new ImageView(context);
给我错误,我不知道为什么!
我的错误日志
06-26 12:21:49.064: E/AndroidRuntime(2806): FATAL EXCEPTION: main
06-26 12:21:49.064: E/AndroidRuntime(2806): java.lang.NullPointerException
06-26 12:21:49.064: E/AndroidRuntime(2806): at com.nazzelha.youm7.DetailsAdapter.getView(DetailsAdapter.java:111)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.widget.ListView.makeAndAddView(ListView.java:1831)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.widget.ListView.fillDown(ListView.java:674)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.widget.ListView.fillFromTop(ListView.java:735)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.widget.ListView.layoutChildren(ListView.java:1652)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.view.View.layout(View.java:14008)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.view.View.layout(View.java:14008)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.view.View.layout(View.java:14008)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.view.View.layout(View.java:14008)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.view.View.layout(View.java:14008)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.view.Choreographer.doFrame(Choreographer.java:532)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.os.Handler.handleCallback(Handler.java:725)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.os.Handler.dispatchMessage(Handler.java:92)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.os.Looper.loop(Looper.java:137)
06-26 12:21:49.064: E/AndroidRuntime(2806): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-26 12:21:49.064: E/AndroidRuntime(2806): at java.lang.reflect.Method.invokeNative(Native Method)
06-26 12:21:49.064: E/AndroidRuntime(2806): at java.lang.reflect.Method.invoke(Method.java:511)
06-26 12:21:49.064: E/AndroidRuntime(2806): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-26 12:21:49.064: E/AndroidRuntime(2806): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-26 12:21:49.064: E/AndroidRuntime(2806): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
这意味着您可能忘记初始化 im (ImageView)数组,该数组应该通过以下方式完成:
ImageView [ ] im = new ImageView [ 3 ];
im[0] = new ImageView(context); //filling one element at a time
temps[1] = ..;
temps[2] = ..;
或使用进行循环:
for(int imgNr = 0; imgNr < 3; imgNr++)
{
//fill one element at a time
im[imgNr] = new ImageView(context);
}
或使用任何其他已知方法进行初始化和填充对象数组。
您必须这样做,因为数组的大小始终在Java中修复。通常不使用数组,而是使用List或任何其他替代方法的实现,不需要大小定义。
如果之后在应用程序中需要数组,可以使用 toArray 方法来检索驻留在List中的对象数组