我在尝试按照本教程时遇到强制关闭错误:
http://www.bango29.com/go/blog/2011/android-asynctask-is-a-beauty-part-2
有什么建议吗?我真的不确定我在哪里出错了。
我的来源:
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 :(得分:0)
有两个问题:
在清单中,您已将HttpGetAndroidExample
类声明为活动。与此相反,HttpGetAndroidExample
类不extend
Activiy
类。所以,它不是一项活动。这就是异常HttpGetAndroidExample cannot be cast to android.app.Activity
的原因。将其扩展为Activity并创建所需的函数,或者如果它不是活动,则将其从清单中删除。
此外,教程链接上没有我可以看到这个课程的地方。