0
我想以編程方式添加多個圖像視圖,這取決於是否有圖像可以查看或不是。所以我在我的ArrayAdapter類中試過這個..我在這裏做的是檢查是否有額外的圖像的任何URL或沒有,然後創建一個數的ImageView的在其上顯示這些圖像通過ArrayAdapter將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)
什麼樣的錯誤,它給你的對象數組?你有沒有指定你的IM數組的長度? –
檢查你的logcat並告訴我們你得到的錯誤。而且錯誤可能是存在的,你不把上下文值放到上下文變量中。 將上下文初始化爲類變量。 在構造函數中放入this.context = context;不管它的第一個參數是什麼 – Android
我從適配器的構造函數中獲取上下文,所以 – vezikon