[英]Force Close when following ASyncTask Example
I'm getting a force close error when attempting to follow this tutorial: 尝试遵循本教程时出现强制关闭错误:
http://www.bango29.com/go/blog/2011/android-asynctask-is-a-beauty-part-2 http://www.bango29.com/go/blog/2011/android-asynctask-is-a-beauty-part-2
Any suggestions? 有什么建议么? I'm really not sure where I went wrong in this. 我真的不确定在哪里出错。
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: 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
MANIFEST: 表现:
<?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>
There are two issues: 有两个问题:
In manifest, you've declared the HttpGetAndroidExample
class as an activity. 在清单中,您已将HttpGetAndroidExample
类声明为活动。 Contradictory to that, the HttpGetAndroidExample
class does not extend
the Activiy
class. 与此HttpGetAndroidExample
, HttpGetAndroidExample
类没有extend
Activiy
类。 So, its not an activity. 因此,这不是一项活动。 That's why the exception HttpGetAndroidExample cannot be cast to android.app.Activity
. 这就是为什么HttpGetAndroidExample cannot be cast to android.app.Activity
异常HttpGetAndroidExample cannot be cast to android.app.Activity
。 Either extend it to be an Activity and create the required functions or remove it from the manifest if its not an activity. 要么将其扩展为活动,然后创建所需的功能,要么将其从清单中删除(如果不是活动的话)。
Also, there is no where on the tutorial link I could see this class. 另外,在教程链接上没有什么地方可以看到此类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.