Java 自定义列表视图捕获异常
参考教程 我正在努力从url获取缩略图。customizedListView类:Java 自定义列表视图捕获异常,java,android,android-intent,nullpointerexception,Java,Android,Android Intent,Nullpointerexception,参考教程 我正在努力从url获取缩略图。customizedListView类: 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"; //
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);
}
});
}
请帮帮我,我做错了什么。logcat给出了这个异常,在emulator上,我收到一个警告,说“不幸的是,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>
![CDATA[新播放列表]]
哈姆德(拉伊拉哈·伊拉拉)
朗诵人:Alhaaj Muhammad Owais Raza Qadri——作者:Mufti-e-Azam Hind Molana Mustafa Raza Khan Noori
http://173.193.24.66/~kanz/video/Images/9.jpg
真的
510
http://173.193.24.66/~kanz/video/flv/9.flv
http://173.193.24.66/~kanz/video/mp4/9.mp4
直接的
两个原因
- 您是否已将清单中的活动声明为:
- 如果不是上述原因,那么您可以检查活动类的名称,其中一定有一些输入错误
内部布局文件
它可能没有找到id为
list
的元素。我使用编辑过的方法getDoElement(在XMLParser类中)解决了这个问题,如下所示:
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
// InputSource is = new InputSource();
// is.setCharacterStream(new StringReader(xml));
doc = db.parse("http://x.x.x.x/~kanz/video/XML/9.xml");
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
我不知道为什么,但在评论完
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
将URL直接传递给db.parse解决了我的问题。希望它也能帮助别人 如果您发布完整的日志消息,这将对人们有所帮助。@android.fryo查看我的更新问题您可以发布完整的定制ListView类吗?日志文件中的行号没有告诉我们任何信息。@coderek它是完整的类。
import
语句不包括在内,我不知道com.example.razatubevidurl.CustomizedListView.onCreate上的(CustomizedListView.java:49)
指向哪一行。@AbdusSamiKhan发布完整日志。查看更新的问题,如果您认为有问题,请告诉我。
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
// InputSource is = new InputSource();
// is.setCharacterStream(new StringReader(xml));
doc = db.parse("http://x.x.x.x/~kanz/video/XML/9.xml");
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));