Почему я получаю это исключение 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)
У меня есть групповое представление кнопок.При нажатии каждая кнопка выполняет следующий фрагмент кода:
public void onClick(View view) {
dft.cancel(true);
dft = new DownloadFilesTask();
dft.execute(URL);
}
внутри DownloadFilesTask класс, который расширяет 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();
}
}
Исключение возникает, когда я нажимаю на кнопки.
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;
}
@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;
}
}
RSSItem.toString () метод:
public String toString() {
if (description.length() > 60) {
return description.substring(0, 60) + "...";
}
return description;
}
Вот ListAdapter инициализация:
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);
ОБНОВЛЕНИЕ:
Хорошо, я вижу, что все ответили, до этого момента неправильно поняли вопрос.Очевидно, что NullPointerException вызывается, когда переменная имеет значение null, и здесь ясно, что при попытке доступа к методу toString () класса RSSItem это причина ошибки.
Что вызывает перекрытие процессов и как его можно предотвратить?