0

表示する画像があるかどうかに応じて、プログラムでいくつかの画像ビューを追加したかったので、ArrayAdapter クラス内でこれを試しました。ここで行っているのは、余分な画像の URL があるかどうかを確認することです。そして、これらの画像を表示するためにいくつかの画像ビューを作成します

@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

これは、おそらくim (ImageView) 配列の初期化を忘れていることを意味します。これは次の方法で行う必要があります。

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

またはforループを使用します。

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

または、オブジェクトの配列を初期化して埋めるための他の既知の方法を使用します。

Javaでは配列のサイズが常に固定されているため、これを行う必要があります。通常、配列を使用する代わりに、サイズ定義を必要としない List またはその他の代替の実装を使用します。

アプリケーションで後で配列が必要な場合は、toArrayメソッドを使用して List にあるオブジェクトの配列を取得できます

于 2013-06-26T11:21:39.987 に答える