0

参考 教程,

我正在努力从网址获取缩略图。定制的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>
4

3 回答 3

0

你有<ListView/>内部布局文件吗?

它可能没有找到 id 的元素list

于 2013-03-27T12:09:04.727 回答
0

我通过使用编辑的方法 getDomElement (在 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解决我的问题。希望它也对其他人有所帮助!

于 2013-04-01T14:10:45.387 回答
0

两个原因

  • 您是否将清单中的活动声明为:

<activity android:name="com.example.razatubevidurl.CustomizedListView"/>

  • 如果不是上述原因,那么您可以检查 Activity 类的名称,一定有一些错字。
于 2013-03-27T10:33:22.177 回答