2013-07-18 45 views
0

我得到試圖遵循本教程時,強制關閉錯誤:強制關閉下面的AsyncTask實例

http://www.bango29.com/go/blog/2011/android-asynctask-is-a-beauty-part-2

有什麼建議?我真的不確定我在哪裏出錯了。

MY SOURCE:

public class HttpGetAndroidExample { 

    // The url of the website. This is just an example 

    private static final String webSiteURL = "http://www.supercars.net/gallery/119513/2841/5.html"; 

    // The path of the folder that you want to save the images to 

    private static final String folderPath = "<FOLDER PATH>"; 

    public static void main(String[] args) { 

     try { 

      // Connect to the website and get the html 

      Document doc = Jsoup.connect(webSiteURL).get(); 

      // Get all elements with img tag , 

      Elements img = doc.getElementsByTag("img"); 

      for (Element el : img) { 

       // for each element get the srs url 
       String src = el.absUrl("src"); 

       System.out.println("Image Found!"); 

       System.out.println("src attribute is : " + src); 

       getImages(src); 

      } 

     } catch (IOException ex) { 

      System.err.println("There was an error"); 

      Logger.getLogger(HttpGetAndroidExample.class.getName()).log(Level.SEVERE, 
        null, ex); 

     } 

    } 

    private static void getImages(String src) throws IOException { 

     String folder = null; 

     // Exctract the name of the image from the src attribute 
     int indexname = src.lastIndexOf("/"); 

     if (indexname == src.length()) { 

      src = src.substring(1, indexname); 

     } 

     indexname = src.lastIndexOf("/"); 

     String name = src.substring(indexname, src.length()); 

     System.out.println(name); 

     // Open a URL Stream 

     URL url = new URL(src); 

     InputStream in = url.openStream(); 

     OutputStream out = new BufferedOutputStream(new FileOutputStream(
       folderPath + name)); 

     for (int b; (b = in.read()) != -1;) { 

      out.write(b); 

     } 

     out.close(); 

     in.close(); 

    } 

} 

的logcat:

07-18 17:22:06.449: D/ActivityThread(11404): setTargetHeapUtilization:0.25 
07-18 17:22:06.449: D/ActivityThread(11404): setTargetHeapIdealFree:8388608 
07-18 17:22:06.449: D/ActivityThread(11404): setTargetHeapConcurrentStart:2097152 
07-18 17:22:06.469: W/dalvikvm(11404): threadid=1: thread exiting with uncaught exception (group=0x41e1f438) 
07-18 17:22:06.469: E/AndroidRuntime(11404): FATAL EXCEPTION: main 
07-18 17:22:06.469: E/AndroidRuntime(11404): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.httpgetandroidexample/com.example.httpgetandroidexample.HttpGetAndroidExample}: java.lang.ClassCastException: com.example.httpgetandroidexample.HttpGetAndroidExample cannot be cast to android.app.Activity 
07-18 17:22:06.469: E/AndroidRuntime(11404): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2012) 
07-18 17:22:06.469: E/AndroidRuntime(11404): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113) 
07-18 17:22:06.469: E/AndroidRuntime(11404): at android.app.ActivityThread.access$700(ActivityThread.java:139) 
07-18 17:22:06.469: E/AndroidRuntime(11404): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224) 
07-18 17:22:06.469: E/AndroidRuntime(11404): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-18 17:22:06.469: E/AndroidRuntime(11404): at android.os.Looper.loop(Looper.java:137) 
07-18 17:22:06.469: E/AndroidRuntime(11404): at android.app.ActivityThread.main(ActivityThread.java:4918) 
07-18 17:22:06.469: E/AndroidRuntime(11404): at java.lang.reflect.Method.invokeNative(Native Method) 
07-18 17:22:06.469: E/AndroidRuntime(11404): at java.lang.reflect.Method.invoke(Method.java:511) 
07-18 17:22:06.469: E/AndroidRuntime(11404): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 
07-18 17:22:06.469: E/AndroidRuntime(11404): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 
07-18 17:22:06.469: E/AndroidRuntime(11404): at dalvik.system.NativeStart.main(Native Method) 
07-18 17:22:06.469: E/AndroidRuntime(11404): Caused by: java.lang.ClassCastException: com.example.httpgetandroidexample.HttpGetAndroidExample cannot be cast to android.app.Activity 
07-18 17:22:06.469: E/AndroidRuntime(11404): at android.app.Instrumentation.newActivity(Instrumentation.java:1068) 
07-18 17:22:06.469: E/AndroidRuntime(11404): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2003) 
07-18 17:22:06.469: E/AndroidRuntime(11404): ... 11 more 

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.httpgetandroidexample" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.httpgetandroidexample.HttpGetAndroidExample" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 

</manifest> 
+0

你是否宣佈他的班級是在清單中的活動?您可能忘記將此課程擴展到活動,或者您錯誤地將其宣佈爲活動。請張貼清單。我在該教程中看不到任何類「HttpGetAndroidExample」。 –

+0

發佈我的清單(感謝您查看此內容!) – AmaniSwann

回答

0

這裏有兩個問題:

清單中,你已經宣佈HttpGetAndroidExample類作爲交流tivity。相反,HttpGetAndroidExample類不extendActiviy類。所以,它不是一項活動。這就是爲什麼例外HttpGetAndroidExample cannot be cast to android.app.Activity。或者將其擴展爲一個Activity並創建所需的函數,或者如果它不是活動,則將其從清單中移除。

此外,沒有在教程鏈接上我可以看到這個類的地方。