このようなコードなのですが、このようなエラーによりエミュレーターのアプリが強制終了してしまいます。 原因を教えていただけないでしょうか?どうぞよろしくお願いします。
java
1package com.example.kanehiro.myapplication2; 2 3import java.io.IOException; 4 import javax.xml.parsers.DocumentBuilder; 5 import javax.xml.parsers.DocumentBuilderFactory; 6 import javax.xml.parsers.ParserConfigurationException; 7 import org.w3c.dom.Document; 8 import org.w3c.dom.Element; 9 import org.w3c.dom.NodeList; 10 import org.xml.sax.SAXException; 11 12public class MainActivity { 13 14 public static void main(String[] args) { 15 String path = "http://www.apple.com/jp/main/rss/hotnews/hotnews.rss"; 16 parseXML(path); 17 } 18 19 public static void parseXML(String path) { 20 try { 21 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 22 DocumentBuilder builder = factory.newDocumentBuilder(); 23 Document document = builder.parse(path); 24 Element root = document.getDocumentElement(); 25 26 /* Get and print Title of RSS Feed. */ 27 NodeList channel = root.getElementsByTagName("channel"); 28 NodeList title = ((Element)channel.item(0)).getElementsByTagName("title"); 29 System.out.println("\nTitle: " + title.item(0).getFirstChild().getNodeValue() + "\n"); 30 31 /* Get Node list of RSS items */ 32 NodeList item_list = root.getElementsByTagName("item"); 33 for (int i = 0; i <item_list.getLength(); i++) { 34 Element element = (Element)item_list.item(i); 35 NodeList item_title = element.getElementsByTagName("title"); 36 NodeList item_link = element.getElementsByTagName("link"); 37 System.out.println(" title: " + item_title.item(0).getFirstChild().getNodeValue()); 38 System.out.println(" link: " + item_link.item(0).getFirstChild().getNodeValue() + "\n"); 39 } 40 } catch (IOException e) { 41 System.out.println("IO Exception"); 42 } catch (ParserConfigurationException e) { 43 System.out.println("Parser Configuration Exception"); 44 } catch (SAXException e) { 45 System.out.println("SAX Exception"); 46 } 47 return; 48 } 49}
java
1FATAL EXCEPTION: main 209-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: Process: com.example.kanehiro.myapplication2, PID: 7939 309-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.kanehiro.myapplication2/com.example.kanehiro.myapplication2.MainActivity}: java.lang.ClassCastException: com.example.kanehiro.myapplication2.MainActivity cannot be cast to android.app.Activity 409-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327) 509-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 609-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java) 709-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 809-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 909-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148) 1009-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417) 1109-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 1209-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 1309-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 1409-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: Caused by: java.lang.ClassCastException: com.example.kanehiro.myapplication2.MainActivity cannot be cast to android.app.Activity 1509-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at android.app.Instrumentation.newActivity(Instrumentation.java:1067) 1609-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317) 1709-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 1809-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java) 1909-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 2009-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 2109-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148) 2209-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417) 2309-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 2409-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 2509-27 19:29:30.247 7939-7939/com.example.kanehiro.myapplication2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 2609-27 19:29:40.798 7939-7945/com.example.kanehiro.myapplication2 W/art: Suspending all threads took: 8.973ms 27
回答2件
あなたの回答
tips
プレビュー