参考 本 教程,
我正在努力从网址获取缩略图。定制的ListView 类:
public class CustomizedListView extends Activity
{
// All static variables
static final String URL = "http://173.193.24.66/~kanz/video/XML/9.xml";
// XML node keyse.
static final String KEY_SONG = "item"; // parent node
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "description";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "image";
ListView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_SONG);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap <String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Context context = getApplicationContext();
Intent in = new Intent(context, SingleMenuItemActivity.class);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(in);
}
});
}
}
我的适配器
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList <HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(CustomizedListView.KEY_TITLE));
artist.setText(song.get(CustomizedListView.KEY_ARTIST));
duration.setText(song.get(CustomizedListView.KEY_DURATION));
imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
return vi;
}
}
而 logcat 给出了 Nullpointer 异常。
03-27 19:16:29.321: E/AndroidRuntime(1516): FATAL EXCEPTION: main
03-27 19:16:29.321: E/AndroidRuntime(1516): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.razatubevidurl/com.example.razatubevidurl.CustomizedListView}: java.lang.NullPointerException
03-27 19:16:29.321: E/AndroidRuntime(1516): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-27 19:16:29.321: E/AndroidRuntime(1516): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-27 19:16:29.321: E/AndroidRuntime(1516): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-27 19:16:29.321: E/AndroidRuntime(1516): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-27 19:16:29.321: E/AndroidRuntime(1516): at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 19:16:29.321: E/AndroidRuntime(1516): at android.os.Looper.loop(Looper.java:137)
03-27 19:16:29.321: E/AndroidRuntime(1516): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-27 19:16:29.321: E/AndroidRuntime(1516): at java.lang.reflect.Method.invokeNative(Native Method)
03-27 19:16:29.321: E/AndroidRuntime(1516): at java.lang.reflect.Method.invoke(Method.java:511)
03-27 19:16:29.321: E/AndroidRuntime(1516): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-27 19:16:29.321: E/AndroidRuntime(1516): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-27 19:16:29.321: E/AndroidRuntime(1516): at dalvik.system.NativeStart.main(Native Method)
03-27 19:16:29.321: E/AndroidRuntime(1516): Caused by: java.lang.NullPointerException
03-27 19:16:29.321: E/AndroidRuntime(1516): at com.example.razatubevidurl.CustomizedListView.onCreate(CustomizedListView.java:49)
03-27 19:16:29.321: E/AndroidRuntime(1516): at android.app.Activity.performCreate(Activity.java:5104)
03-27 19:16:29.321: E/AndroidRuntime(1516): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-27 19:16:29.321: E/AndroidRuntime(1516): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-27 19:16:29.321: E/AndroidRuntime(1516): ... 11 more
请帮我解决我做错了什么。logcat 给出了这个异常,在模拟器上我收到一条警告说“不幸的是,myApp 已停止”任何帮助将不胜感激。谢谢。PS:我已经提到了 Manifest 中的所有活动,并且也拥有所有权限。
编辑:这是我的 XML:
<playlist><title>![CDATA[New Playlist]]</title>
<items>
<item>
<title>HAMD(LA ILAHA ILLALLAH)</title>
<description>Recited By :Alhaaj Muhammad Owais Raza Qadri -- Written By: Mufti-e-Azam Hind Molana Mustafa Raza Khan Noori</description>
<image>http://173.193.24.66/~kanz/video/Images/9.jpg</image>
<startFromThis>true</startFromThis>
<duration>510</duration>
<source>http://173.193.24.66/~kanz/video/flv/9.flv</source>
<sourceAlt>http://173.193.24.66/~kanz/video/mp4/9.mp4</sourceAlt>
<sourceType>direct</sourceType>
</item>
</items>
</playlist>