我正在RSS Feed上开发一个应用程序。 现在写我得到的所有东西,但从xml获取图像的问题。
我在
中遇到了nullpointer错误package com.xmlparse;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class EfficientAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<Post> data;
private ImageView iv;
private static LayoutInflater inflater = null;
// public ImageLoader imageLoader;
ViewHolder holder;
EfficientAdapter(Activity a, ArrayList<Post> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// imageLoader = new ImageLoader(activity.getApplicationContext());
}
@Override
public int getCount() {
return data.toArray().length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView label;
public TextView addr;
public ImageView image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.label = (TextView) vi.findViewById(R.id.title);
holder.addr = (TextView) vi.findViewById(R.id.details);
holder.image = (ImageView) vi.findViewById(R.id.thumb);
vi.setTag(holder);
} else
holder = (ViewHolder) vi.getTag();
holder.label.setText(data.get(position).getTitle());
holder.addr.setText(data.get(position).getPubDate());
// imageLoader.DisplayImage((data.get(position).getThumbnail()),
// activity,
// holder.image, 72, 72);
URL url = null;
try {
url = new URL((data.get(position).getThumbnail()));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//我在这里遇到错误。你可以在log cat中看到错误。
InputStream content = null;
try {
content = (InputStream) url.getContent();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Drawable d = Drawable.createFromStream(content, "src");
Bitmap mIcon1 = null;
try {
mIcon1 = BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
holder.image.setImageBitmap(Bitmap.createScaledBitmap(mIcon1, 72, 72,
false));
return vi;
}
}
logcat的
04-07 15:15:15.092: E/AndroidRuntime(1225): FATAL EXCEPTION: main
04-07 15:15:15.092: E/AndroidRuntime(1225): java.lang.NullPointerException
04-07 15:15:15.092: E/AndroidRuntime(1225): at com.xmlparse.EfficientAdapter.getView(EfficientAdapter.java:92)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.widget.ListView.makeAndAddView(ListView.java:1745)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.widget.ListView.fillDown(ListView.java:670)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.widget.ListView.fillFromTop(ListView.java:727)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.widget.ListView.layoutChildren(ListView.java:1598)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.view.View.layout(View.java:7175)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.view.View.layout(View.java:7175)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.view.View.layout(View.java:7175)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.view.View.layout(View.java:7175)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.view.View.layout(View.java:7175)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.os.Looper.loop(Looper.java:123)
04-07 15:15:15.092: E/AndroidRuntime(1225): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-07 15:15:15.092: E/AndroidRuntime(1225): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 15:15:15.092: E/AndroidRuntime(1225): at java.lang.reflect.Method.invoke(Method.java:507)
04-07 15:15:15.092: E/AndroidRuntime(1225): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-07 15:15:15.092: E/AndroidRuntime(1225): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-07 15:15:15.092: E/AndroidRuntime(1225): at dalvik.system.NativeStart.main(Native Method)