Android - 无法从url字符串打开浏览器Intent

时间:2012-02-20 22:28:39

标签: android android-intent android-browser intentservice

我正在使用XML并将URL作为字符串变量收集。我得到了正确的链接,我可以在LogCat中设置它。

但是,当我设置onItemClickListener时;它不识别链接并抛出ActivityNotFoundException。

这是代码。

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    final String announcementsTitle="announcements";
    final String announcementsLink = "link";

    ArrayList<HashMap<String, String>> announcementsList = new ArrayList<HashMap<String, String>>();

    try
    {

        URL url = new URL("http://www.myexperiment.org/announcements.xml?num=100&order=reverse");

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();

        Document doc = db.parse(new InputSource(url.openStream()));
        doc.getDocumentElement().normalize();

        NodeList nodeList1 = doc.getElementsByTagName("announcement");


        for(int i=0; i < nodeList1.getLength(); i++)
        {
            //Creating HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            Node node = nodeList1.item(i);

            //For the announcement title
            Element firstElement = (Element) node;
            NodeList nameList = firstElement.getElementsByTagName("announcement");

            Element nameElement = (Element) nameList.item(0);
            nameList = nameElement.getChildNodes();

            String link1 = firstElement.getAttributeNode("resource").getNodeValue();


            //Adding values of each into HashMap
            map.put(announcementsTitle,((Node) nameList.item(0)).getNodeValue());
            map.put(announcementsLink,link1);

            String title = nameList.item(0).getNodeValue();


            Log.d("Announcements: ", title);
            Log.d("Link: ", link1);

            //Adding HashList to ArrayList
            announcementsList.add(map);

        }//for

        ListAdapter adapter = new SimpleAdapter(this, announcementsList,R.layout.announcements,
                                 new String[] {announcementsTitle, announcementsLink}, 
                                 new int[] {R.id.announcementTitle});
        setListAdapter(adapter);

        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
            {

                //String the new Intent open the browser with link
                Log.d("Link: ", announcementsLink);

                final Intent announcementsIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(announcementsLink));
                startActivity(announcementsIntent);
            }

        });

    }
    catch(Exception e)
    {
        System.out.println("XML Parsing Exeption = " + e);
    }

}//onCreate

以下是来自LogCat的消息

02-20 22:15:43.203: D/Link:(919): http://www.myexperiment.org/announcements/2  
02-20 22:15:43.203: D/Announcements:(919): New announcements feature!  
02-20 22:15:43.203: D/Link:(919): http://www.myexperiment.org/announcements/1  
02-20 22:15:50.163: D/Link:(919): link  
02-20 22:15:50.243: D/AndroidRuntime(919): Shutting down VM  
02-20 22:15:50.243: W/dalvikvm(919): threadid=1: thread exiting with uncaught exception (group=0x40015560)  
02-20 22:15:50.313: E/AndroidRuntime(919): FATAL EXCEPTION: main  
02-20 22:15:50.313: E/AndroidRuntime(919): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=link }  
02-20 22:15:50.313: E/AndroidRuntime(919):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409)  
02-20 22:15:50.313: E/AndroidRuntime(919):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)  
02-20 22:15:50.313: E/AndroidRuntime(919):  at android.app.Activity.startActivityForResult(Activity.java:2827)  
02-20 22:15:50.313: E/AndroidRuntime(919):  at android.app.Activity.startActivity(Activity.java:2933) 

1 个答案:

答案 0 :(得分:0)

您将announcementsLink变量的值作为URI传递给startActivity。此变量的值始终为"link"。这不是有效的URI。我猜你想要做的是取得与点击的ListItem相关联的URI,你没有这样做。您可以获取为特定列表项存储的链接,如下所示:

lv.setOnItemClickListener(new OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {
        final String itemLink = adapter.getItem(potition).get(announcementsLink)
        final Intent announcementsIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(itemLink));
        startActivity(announcementsIntent);
    }
});

免责声明,我自己没有测试过这段代码,但总体思路就在那里。