0

我想以编程方式添加几个图像视图,这取决于是否有要查看的图像..所以我在我的 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)
4

1 回答 1

0

This means that you probably forgot to initialize your im (ImageView) array, which should be done by:

ImageView [ ] im = new ImageView [ 3 ];
im[0] = new ImageView(context);  //filling one element at a time
temps[1] = ..;
temps[2] = ..;

or use for loop:

for(int imgNr = 0; imgNr < 3; imgNr++)
{
      //fill one element at a time
      im[imgNr] = new ImageView(context);    
}

or use any other known method for initialization and filling up an array of objects.

You have to do this because array's size is always fixed in Java. Typically instead of using an array, you'd use an implementation of List or any other alternative, which does not need the size definition.

If array is needed afterwards in application, toArray method may be used to retrieve array of objects residing in List

于 2013-06-26T11:21:39.987 回答