• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Caused by: java.lang.ClassCastException: android.app.Application

Letfly

Lurker
Hi! I have class ContactClass
ContactClass.java
Code:
package com.app.whatisylife;
import android.content.Context;
import android.database.Cursor;
import java.text.SimpleDateFormat;
import java.util.Locale;

import android.app.Activity;

class ContactClass 
{   
	private Context myContext; 
	private Cursor cursor;
	ContactClass(Context context){myContext=context;}
    public void getCallsInfo() 
    {      
        cursor = myContext.getContentResolver().query(        		 
                android.provider.CallLog.Calls.CONTENT_URI,
                null, null, null,
                android.provider.CallLog.Calls.DATE + " DESC");
        ((Activity) myContext).startManagingCursor(cursor);
        int numberColumn = cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
        int dateColumn = cursor.getColumnIndex(android.provider.CallLog.Calls.DATE);
        int typeColumn = cursor.getColumnIndex(android.provider.CallLog.Calls.TYPE);
        String[] callList = new String[cursor.getCount()];
        
        while(cursor.moveToNext()) 
        {
        	String callerPhoneNumber = cursor.getString(numberColumn);
        	int callDate = cursor.getInt(dateColumn);
            int callType = cursor.getInt(typeColumn);	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
            String dateString = sdf.format(callDate);
            callList[cursor.getPosition()]=callerPhoneNumber+callDate+callType+dateString;
        }
        cursor.close();
    }
}

Here i create ContactClass object

Code:
package com.app.whatisylife;
import android.app.Activity;
import android.os.Bundle;

public class whatIsYourLife extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        ContactClass cc = new ContactClass(getApplicationContext());
        cc.getCallsInfo();
        
    }
}

Error: Caused by: java.lang.ClassCastException: android.app.Application.


03-11 06:16:26.691: WARN/dalvikvm(228): threadid=3: thread exiting with uncaught exception (group=0x4001b188)

03-11 06:16:26.691: ERROR/AndroidRuntime(228): Uncaught handler: thread main exiting due to uncaught exception

03-11 06:16:26.722: ERROR/AndroidRuntime(228): java.lang.RuntimeException: Unable to start activity

ComponentInfo{com.app.whatisylife/com.app.whatisylife.whatIsYourLife}: java.lang.ClassCastException:

android.app.Application

03-11 06:16:26.722: ERROR/AndroidRuntime(228): at

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)

03-11 06:16:26.722: ERROR/AndroidRuntime(228): at

android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)

03-11 06:16:26.722: ERROR/AndroidRuntime(228): at

android.app.ActivityThread.access$2200(ActivityThread.java:119)

03-11 06:16:26.722: ERROR/AndroidRuntime(228): at

android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)

03-11 06:16:26.722: ERROR/AndroidRuntime(228): at android.os.Handler.dispatchMessage(Handler.java:99)

03-11 06:16:26.722: ERROR/AndroidRuntime(228): at android.os.Looper.loop(Looper.java:123)

03-11 06:16:26.722: ERROR/AndroidRuntime(228): at android.app.ActivityThread.main(ActivityThread.java:4363)
03-11 06:16:26.722: ERROR/AndroidRuntime(228): at java.lang.reflect.Method.invokeNative(Native Method)

03-11 06:16:26.722: ERROR/AndroidRuntime(228): at java.lang.reflect.Method.invoke(Method.java:521)

03-11 06:16:26.722: ERROR/AndroidRuntime(228): at

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)

03-11 06:16:26.722: ERROR/AndroidRuntime(228): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-11 06:16:26.722: ERROR/AndroidRuntime(228): at dalvik.system.NativeStart.main(Native Method)

03-11 06:16:26.722: ERROR/AndroidRuntime(228): Caused by: java.lang.ClassCastException: android.app.Application
03-11 06:16:26.722: ERROR/AndroidRuntime(228): at

com.app.whatisylife.ContactClass.getCallsInfo(ContactClass.java:20)

03-11 06:16:26.722: ERROR/AndroidRuntime(228): at

com.app.whatisylife.whatIsYourLife.onCreate(whatIsYourLife.java:14)

03-11 06:16:26.722: ERROR/AndroidRuntime(228): at

android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

03-11 06:16:26.722: ERROR/AndroidRuntime(228): at

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)

03-11 06:16:26.722: ERROR/AndroidRuntime(228): ... 11 more

03-11 06:16:26.743: INFO/Process(60): Sending signal. PID: 228 SIG: 3

03-11 06:16:26.743: INFO/dalvikvm(228): threadid=7: reacting to signal 3

03-11 06:16:26.743: ERROR/dalvikvm(228): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
03-11 06:16:27.111: DEBUG/ddm-heap(228): Got feature list request

Help to manage please?
 
The exception is saying, this line is bad:
Code:
((Activity) myContext).startManagingCursor(cursor);
Which it is. Because ContactClass is not an Activity, and you can not use any Activity methods unless ContactClass would have been an Activity.

If you instead just remove the bad line: '((Activity) myContext).startManagingCursor(cursor);' the whole thing would work. If you want Android to take care of handling the lifecycle of the cursor for you, then it must be done in an Activity!
 
Back
Top Bottom