-2

I'm getting the following force close error: java.lang.ClassCastException: com.example.httpgetandroidexample.HttpGetAndroidExample cannot be cast to android.app

When attempting to execute the following code and I'm unsure why.

package com.example.httpgetandroidexample;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class HttpGetAndroidExample {

 public static void main(String[] args) {

    Document doc;
    try {

        // need http protocol
        doc = Jsoup.connect("http://www.sheriff.org/apps/arrest/results.cfm?lname=smith&fname=").get();

        // get page title
        String title = doc.title();
        System.out.println("Arrest # : " + title);

        // get all links
        Elements links = doc.select("a[href]");
        for (Element link : links) {

            // get the value from href attribute
            System.out.println("\nlink : " + link.attr("href"));
            System.out.println("text : " + link.text());

        }

    } catch (IOException e) {
        e.printStackTrace();
    }

 }

}

LOGCAT:

07-18 15:41:43.330: D/ActivityThread(8349): setTargetHeapUtilization:0.25
07-18 15:41:43.330: D/ActivityThread(8349): setTargetHeapIdealFree:8388608
07-18 15:41:43.330: D/ActivityThread(8349): setTargetHeapConcurrentStart:2097152
07-18 15:41:43.350: W/dalvikvm(8349): threadid=1: thread exiting with uncaught exception (group=0x41e1f438)
07-18 15:41:43.350: E/AndroidRuntime(8349): FATAL EXCEPTION: main
07-18 15:41:43.350: E/AndroidRuntime(8349): 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 15:41:43.350: E/AndroidRuntime(8349):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2012)
07-18 15:41:43.350: E/AndroidRuntime(8349):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113)
07-18 15:41:43.350: E/AndroidRuntime(8349):     at android.app.ActivityThread.access$700(ActivityThread.java:139)
07-18 15:41:43.350: E/AndroidRuntime(8349):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224)
07-18 15:41:43.350: E/AndroidRuntime(8349):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 15:41:43.350: E/AndroidRuntime(8349):     at android.os.Looper.loop(Looper.java:137)
07-18 15:41:43.350: E/AndroidRuntime(8349):     at android.app.ActivityThread.main(ActivityThread.java:4918)
07-18 15:41:43.350: E/AndroidRuntime(8349):     at java.lang.reflect.Method.invokeNative(Native Method)
07-18 15:41:43.350: E/AndroidRuntime(8349):     at java.lang.reflect.Method.invoke(Method.java:511)
07-18 15:41:43.350: E/AndroidRuntime(8349):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
07-18 15:41:43.350: E/AndroidRuntime(8349):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
07-18 15:41:43.350: E/AndroidRuntime(8349):     at dalvik.system.NativeStart.main(Native Method)
07-18 15:41:43.350: E/AndroidRuntime(8349): Caused by: java.lang.ClassCastException: com.example.httpgetandroidexample.HttpGetAndroidExample cannot be cast to android.app.Activity
07-18 15:41:43.350: E/AndroidRuntime(8349):     at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
07-18 15:41:43.350: E/AndroidRuntime(8349):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2003)
07-18 15:41:43.350: E/AndroidRuntime(8349):     ... 11 more
07-18 15:42:53.765: D/ActivityThread(8635): setTargetHeapUtilization:0.25
07-18 15:42:53.765: D/ActivityThread(8635): setTargetHeapIdealFree:8388608
07-18 15:42:53.765: D/ActivityThread(8635): setTargetHeapConcurrentStart:2097152
07-18 15:42:53.785: W/dalvikvm(8635): threadid=1: thread exiting with uncaught exception (group=0x41e1f438)
07-18 15:42:53.785: E/AndroidRuntime(8635): FATAL EXCEPTION: main
07-18 15:42:53.785: E/AndroidRuntime(8635): 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 15:42:53.785: E/AndroidRuntime(8635):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2012)
07-18 15:42:53.785: E/AndroidRuntime(8635):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113)
07-18 15:42:53.785: E/AndroidRuntime(8635):     at android.app.ActivityThread.access$700(ActivityThread.java:139)
07-18 15:42:53.785: E/AndroidRuntime(8635):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224)
07-18 15:42:53.785: E/AndroidRuntime(8635):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 15:42:53.785: E/AndroidRuntime(8635):     at android.os.Looper.loop(Looper.java:137)
07-18 15:42:53.785: E/AndroidRuntime(8635):     at android.app.ActivityThread.main(ActivityThread.java:4918)
07-18 15:42:53.785: E/AndroidRuntime(8635):     at java.lang.reflect.Method.invokeNative(Native Method)
07-18 15:42:53.785: E/AndroidRuntime(8635):     at java.lang.reflect.Method.invoke(Method.java:511)
07-18 15:42:53.785: E/AndroidRuntime(8635):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
07-18 15:42:53.785: E/AndroidRuntime(8635):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
07-18 15:42:53.785: E/AndroidRuntime(8635):     at dalvik.system.NativeStart.main(Native Method)
07-18 15:42:53.785: E/AndroidRuntime(8635): Caused by: java.lang.ClassCastException: com.example.httpgetandroidexample.HttpGetAndroidExample cannot be cast to android.app.Activity
07-18 15:42:53.785: E/AndroidRuntime(8635):     at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
07-18 15:42:53.785: E/AndroidRuntime(8635):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2003)
07-18 15:42:53.785: E/AndroidRuntime(8635):     ... 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>
AmaniSwann
  • 15
  • 1
  • 7
  • possible duplicate of [ClassCastException: cannot be cast to android.app.Activity](http://stackoverflow.com/questions/15527850/classcastexception-cannot-be-cast-to-android-app-activity) among numerous others. – Brian Roach Jul 18 '13 at 19:57
  • Building an android app is not the same thing as build a Java application. Start with android training – lory105 Jul 18 '13 at 20:22

1 Answers1

2

You try to run a "simple" java application on Android wich you CAN'T. Start by reading the android documentation . Your application must contain at least one Activity class and you can't put public static void main(String[] args) in any of your classes.

Matthieu Harlé
  • 739
  • 1
  • 13
  • 32
  • I'm attempting to follow this jsoup example - how can this be done? http://www.mkyong.com/java/jsoup-html-parser-hello-world-examples/ – AmaniSwann Jul 18 '13 at 20:13
  • Put what is inside your main method in a method of your choice then call this method from the `onCreate` method of your Activity. But you can't easily see your application console output, you can use [Log](http://developer.android.com/reference/android/util/Log.html) in place of `System.out.println`. If you didn't understand of any of what I said, then I recommend you again to check the [android documentation](http://developer.android.com/training/index.html) – Matthieu Harlé Jul 18 '13 at 20:20
  • I understood - corrected the issue and implemented ASyncTask - but I'm still having a few issues :) http://stackoverflow.com/questions/17733413/the-method-findviewbyidint-is-undefined-for-the-type-jsoup-query – AmaniSwann Jul 18 '13 at 20:45