自定义列表视图的Andr​​oid捕捉异常

自定义列表视图的Andr​​oid捕捉异常

本文介绍了自定义列表视图的Andr​​oid捕捉异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

指的是教程,

我的工作得到从URL缩略图。该customizedListView类:

 公共类CustomizedListView扩展活动{
//所有静态变量
静态最终字符串URL =htt​​p://173.193.24.66/~kanz/video/XML/9.xml;
// XML节点keyse。
静态最后弦乐KEY_SONG =项; //父节点
静态最后弦乐KEY_TITLE =称号;
静态最后弦乐KEY_ARTIST =说明;
静态最后弦乐KEY_DURATION =时间;
静态最后弦乐KEY_THUMB_URL =形象;
ListView控件列表;
LazyAdapter适配器;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    ArrayList的<&HashMap的LT;字符串,字符串>> songsList =新的ArrayList<&HashMap的LT;字符串,字符串>>();    XMLParser的解析器=新XMLParser的();
    字符串XML = parser.getXmlFromUrl(URL); // URL从XML获得
    文档的文档= parser.getDomElement(XML); //获得DOM元素    节点列表NL = doc.getElementsByTagName(KEY_SONG);
    //遍历所有歌曲节点&LT循环;宋>
    的for(int i = 0; I< nl.getLength();我++){
        //创建新的HashMap
        HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
        元素e =(元)nl.item(I)
        //将每个子节点的HashMap键=>值
        map.put(KEY_TITLE,parser.getValue(即KEY_TITLE));
        map.put(KEY_ARTIST,parser.getValue(即KEY_ARTIST));
        map.put(KEY_DURATION,parser.getValue(即KEY_DURATION));
        map.put(KEY_THUMB_URL,parser.getValue(即KEY_THUMB_URL));        //添加HashList到ArrayList的
        songsList.add(地图);
    }    名单=(ListView控件)findViewById(R.id.list);    //通过传递XML数据的ArrayList获取适配器
    适配器=新LazyAdapter(这一点,songsList);
    list.setAdapter(适配器);    // Click事件的单排列表
    list.setOnItemClickListener(新OnItemClickListener(){        @覆盖
        公共无效onItemClick(适配器视图<>母公司,观景,
                INT位置,长的id){
            上下文的背景下= getApplicationContext();
             在意向=新意图(背景下,SingleMenuItemActivity.class);
             in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             context.startActivity(在);        }
    });
}

}

我的适配器

 公共类LazyAdapter延伸BaseAdapter {
私人活动活动;
私人的ArrayList<&HashMap的LT;字符串,字符串>>数据;
私有静态LayoutInflater吹气= NULL;
公共ImageLoader的ImageLoader的;公共LazyAdapter(活动一,ArrayList的<&HashMap的LT;字符串,字符串> D 1和D){
    活性=一个;
    数据= D;
    吹气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ImageLoader的=新ImageLoader的(activity.getApplicationContext());
}公众诠释的getCount(){
    返回data.size();
}公共对象的getItem(INT位置){
    返回的位置;
}众长getItemId(INT位置){
    返回的位置;
}公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    查看VI = convertView;
    如果(convertView == NULL)
        VI = inflater.inflate(R.layout.list_row,NULL);    TextView的标题=(TextView中)vi.findViewById(R.id.title); //称号
    TextView的艺术家=(TextView中)vi.findViewById(R.id.artist); //歌手名
    TextView的持续时间=(TextView中)vi.findViewById(R.id.duration); //持续时间
    ImageView的thumb_image =(ImageView的)vi.findViewById(R.id.list_image); //拇指图像    HashMap的<字符串,字符串>歌=新的HashMap<字符串,字符串>();
    曲= data.get(位置);    //设置列表视图中的所有值
    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);
    返回VI;
}

}

虽然logcat的给空指针异常。

  03-27 19:16:29.321:E / AndroidRuntime(1516):致命异常:主要
03-27 19:16:29.321:E / AndroidRuntime(1516):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.razatubevidurl / com.example.razatubevidurl.CustomizedListView}:显示java.lang.NullPointerException
03-27 19:16:29.321:E / AndroidRuntime(1516):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-27 19:16:29.321:E / AndroidRuntime(1516):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-27 19:16:29.321:E / AndroidRuntime(1516):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
03-27 19:16:29.321:E / AndroidRuntime(1516):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)
03-27 19:16:29.321:E / AndroidRuntime(1516):在android.os.Handler.dispatchMessage(Handler.java:99)
03-27 19:16:29.321:E / AndroidRuntime(1516):在android.os.Looper.loop(Looper.java:137)
03-27 19:16:29.321:E / AndroidRuntime(1516):在android.app.ActivityThread.main(ActivityThread.java:5041)
03-27 19:16:29.321:E / AndroidRuntime(1516):在java.lang.reflect.Method.invokeNative(本机方法)
03-27 19:16:29.321:E / AndroidRuntime(1516):在java.lang.reflect.Method.invoke(Method.java:511)
03-27 19:16:29.321:E / AndroidRuntime(1516):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
03-27 19:16:29.321:E / AndroidRuntime(1516):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-27 19:16:29.321:E / AndroidRuntime(1516):在dalvik.system.NativeStart.main(本机方法)
03-27 19:16:29.321:E / AndroidRuntime(1516):因:显示java.lang.NullPointerException
03-27 19:16:29.321:E / AndroidRuntime(1516):在com.example.razatubevidurl.CustomizedListView.onCreate(CustomizedListView.java:49)
03-27 19:16:29.321:E / AndroidRuntime(1516):在android.app.Activity.performCreate(Activity.java:5104)
03-27 19:16:29.321:E / AndroidRuntime(1516):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-27 19:16:29.321:E / AndroidRuntime(1516):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-27 19:16:29.321:E / AndroidRuntime(1516):11 ...更多

请帮助我,我在做什么错。在logcat中给出了这样的例外和仿真器,我得到一个警告说:不幸的是,对myApp已停止任何帮助将是非常美联社preciated。谢谢。 PS:我已经提到清单中的所有活动,并拥有所有权限过于

编辑:这是我的XML:

 <&播放GT;<标题>![CDATA [新建播放列表]< /标题>
<项目>
<项目>
<标题> HAMD(LA ILAHA ILLALLAH)LT; /标题>
<描述>背诵者:Alhaaj穆罕默德Owais拉扎Qadri - 撰文:穆夫提-E-阿扎姆欣德Molana穆斯塔法·拉扎·汗努里< /描述>
&lt;图像&GT; HTTP://173.193.24.66/~kanz/video/Images/9.jpg< /图片&GT;&LT; startFromThis&GT;真&LT; / startFromThis&GT;
&LT;持续时间GT 510 LT; /持续时间GT;
&lt;信源&GT; HTTP://173.193.24.66/~kanz/video/flv/9.flv< /源&GT;
&LT; sourceAlt&GT; HTTP://173.193.24.66/~kanz/video/mp4/9.mp4< / sourceAlt&GT;&LT;&sourceType的GT;直接&LT; / sourceType的&GT;
&LT; /项目&GT;
&LT; /项目&GT;
&LT; /播放列表&GT;


解决方案

我通过编辑方法getDomElement(以XMLParser的类)解决了这个问题是这样的:

 公开文件getDomElement(字符串XML){
 文档DOC = NULL;
 DBF的DocumentBuilderFactory = DocumentBuilderFactory.newInstance();
 尝试{        的DocumentBuilder分贝= dbf.newDocumentBuilder();    // InputSource的是=新的InputSource();
      // is.setCharacterStream(新StringReader(XML));
            文档= db.parse(HTTP://x.x.x.x/~kanz/video/XML/9.xml);        }赶上(的ParserConfigurationException E){
            Log.e(错误,e.getMessage());
            返回null;
        }赶上(SAXException的E){
            Log.e(错误,e.getMessage());
            返回null;
        }赶上(IOException异常五){
            Log.e(错误,e.getMessage());
            返回null;
        }        返回文档;
}

我不知道为什么,但注释掉后

 的InputSource是=新的InputSource();
is.setCharacterStream(新StringReader(XML));

和直接传递的URL db.parse 解决我的问题。希望它可以帮助别人呢!

Refering to This tutorial,

I'm working to get thumbnails from a url. The customizedListView class:

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);

        }
    });
}

}

My adapter

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;
}

}

While the logcat gives Nullpointer exception.

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

Please help me with what I'm doing wrong. The logcat gives this exception and on emulator I get an alert saying "Unfortunately, myApp has stopped" Any help would be highly appreciated. Thanks. PS: I've mentioned all Activities in Manifest and have all the permissions too.

EDIT: Here's my 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>
解决方案

I solved the issue by using the edited method getDomElement (in XMLParser class) like this:

 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;
}

I don't know why, but after commenting out the

InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));

and passing the URL directly to db.parse solved my problem. Hope it helps others too!

这篇关于自定义列表视图的Andr​​oid捕捉异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:27