开发者

NullPointerException using AsyncTask to download server's data and apply it to a list-adapter

Why do I get this NullPointerException?

08-08 11:07:15.669: ERROR/AndroidRuntime(659): Uncaught handler: thread main exiting due to uncaught exception
08-08 11:07:15.688: ERROR/AndroidRuntime(659): java.lang.NullPointerException
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at com.news.reader.RSSItem.toString(RSSItem.java:63)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at com.news.reader.Home$ListAdapter.getView(Home.java:367)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.widget.AbsListView.obtainView(AbsListView.java:1255)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.widget.ListView.makeAndAddView(ListView.java:1658)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.widget.ListView.fillDown(ListView.java:637)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.widget.ListView.fillSpecific(ListView.java:1224)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.widget.ListView.layoutChildren(ListView.java:1494)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.widget.AbsListView.onLayout(AbsListView.java:1112)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.view.View.layout(View.java:6569)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1108)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.widget.LinearLayout.onLayout(LinearLayout.java:920)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.view.View.layout(View.java:6569)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.view.View.layout(View.java:6569)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.view.View.layout(View.java:6569)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.view.View.layout(View.java:6569)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.view.ViewRoot.performTraversals(ViewRoot.java:979)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1613)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.os.Looper.loop(Looper.java:123)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at android.app.ActivityThread.main(ActivityThread.java:4203)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at java.lang.reflect.Method.invokeNative(Native Method)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at java.lang.reflect.Method.invoke(Method.java:521)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
08-08 11:07:15.688: ERROR/AndroidRuntime(659):     at dalvik.system.NativeStart.main(Native Method)

I have a group-view of buttons. On click every button executes the following piece of code:

public void onClick(View view) {
            dft.cancel(true);
            dft = new DownloadFilesTask();
            dft.execute(URL);
        }

inside DownloadFilesTask class, which extends AsyncTask:

private class DownloadFilesTask extends AsyncTask<String, RSSItem, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            la.clear(); //clears list-adapter's content
        }
        @Override
        protected Void doInBackground(String... urls) {
            try {
                parse(urls[0]);
            } catch (MalformedURLException e) {
                error = resources.getString(R.string.MalformedURLException);
                Log.e(TAG, error);
                return error;
            } catch (IOException e) {
                error = resources.getString(R.string.IOException);
                Log.e(TAG, error);
                return error;
            } catch (XmlPullParserException e) {
                error = resources.getString(R.string.XmlPullParserException);
                Log.e(TAG, error);
                return error;
            }

            if (feed == null) {
                return null;
            }

            rssList = feed.getAllItems();
            Iterator<RSSItem> iterator = rssList.iterator();

            while (iterator.hasNext()) {
                RSSItem rss = iterator.next();
                publishProgress(rss);
            }

            Drawable downloadedImage;
            for (int z = 0; z < rssList.size(); z++) {
                if (isCancelled()) {
                    return null;
                }
                downloadedImage = downloadImage(rssList.get(z).getImageURL());
                ((RSSItem) rssList.get(z)).setImage(downloadedImage);
            }

            return null;
        }
        @Override
        protected void onProgressUpdate(RSSItem... progress) {
            super.onProgressUpdate(progress);

            la.add((RSSItem) progress[0]);
        }
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute();

            SharedProperties.getInstance().setRssList(rssList);
            la.notifyDataSetChanged();
        }
        @Override
        protected void onCancelled() {
            super.onCancelled();
        }
}

The exception pops up when I'm clicking through the buttons.

private class ListAdapter extends ArrayAdapter<RSSItem> {
        private List<RSSItem> items;
        public ListAdapter(Context context, int resourceId,
                List<RSSItem> items) {
            super(context, resourceId, items);
            this.items = items;
        }
    开发者_如何学Python    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;

            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.list_item, null);
            }

            RSSItem item = items.get(position);

            if (item != null) {
                ImageView itemImage = (ImageView) v.findViewById(R.id.ivImage);
                TextView title = (TextView) v.findViewById(R.id.tvTitle);
                TextView description = (TextView) v
                        .findViewById(R.id.tvDescription);
                if (item.getImage() != null) {
                    itemImage.setBackgroundDrawable(item.getImage());
                } else {
                    itemImage.setBackgroundDrawable(getResources().getDrawable(
                            R.drawable.default_img));
                }
                if (title != null) {
                    title.setText(item.getTitle());
                }
                if (description != null) {
                    description.setText(item.toString()); //this is the line which throws the exception
                }
            }

            return v;
        }
    }

The RSSItem.toString() method:

public String toString() {
        if (description.length() > 60) {
            return description.substring(0, 60) + "...";
        }
        return description;
    }

Here is the ListAdapter initialization:

rssList = new Vector<RSSItem>();

lv = (ListView) findViewById(R.id.bListNews);
lv.setOnItemClickListener(listItemClick);

la = new ListAdapter(this, R.layout.list_item, rssList);
lv.setAdapter(la);

UPDATE:

Ok, I do see that everyone answered, until this moment, misunderstood the question. It is obvious that the NullPointerException it's caused when a variable is null and here it's clear that trying to access the toString() method of RSSItem class it's the reason of the error.

What do cause the overlapping of processes and how could be prevented?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜