この チュートリアルを参考に 、
URLからサムネイルを取得するために取り組んでいます。カスタマイズされた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: マニフェストですべてのアクティビティについて言及し、すべての権限も持っています。
編集:ここに私の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>